zoukankan      html  css  js  c++  java
  • 0301——UItableView

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];分组形式的

        self.myTableView.delegate = self;

        self.myTableView.dataSource = self;     <UITableViewDelegate,UITableViewDataSource>两个代理都要遵守

        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;     分割线

        [self.myTableView setEditing:YES];     编辑状态

        [self.view addSubview:_myTableView];

    }

    1.有多少段 默认为0

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return 26;

    }

    2.有多少行,调用多次

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        if ((section+1)%2 == 0) {

            return 3;

        }else{

            return 2;

        }

    }

    3.某段某行显示什么样子  NSIndexPath封装的两个属性sectionrow

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        //重复利用机制

        NSString *cellID = @"cellID";

        //先判断是否有可以重复利用的cell

        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

        if (cell==nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        }

        //cell显示的样式

        cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];

        return cell;

    }

    4.配置cell高度

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 80;

    }

    5.段落头部尾部

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

        return @"头标题";

    }

    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

        return @"尾标题";

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

        return 30;

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

        return 30;//不能没有,如果想没有就把值变小

    }

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

        UIView * v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

        v.backgroundColor = [UIColor redColor];

        return v;

    }

    6.cell被点了要做什么

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        [tableView deselectRowAtIndexPath:indexPath animated:YES];取消点击事件

    }

    7.设置cell是否可以编辑

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row== 0) {

            return NO;

        }else{

            return YES;

        }

    }

    8.更改编辑状态的按钮

    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

        return  UITableViewCellEditingStyleDelete;

    }

    9.更改删除状态下右边显示的标题

    -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

        return @"删除";

    }

    10.删除状态下按钮被点

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    }

    11.cell可以上下移动

    -(bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

        return YES;

    }

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  

    }

    12.索引标题

    -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

        NSMutableArray * titleArray = [NSMutableArray array];

        for (int i = 0; i<26; i++) {

            [titleArray addObject:[NSString stringWithFormat:@"%c",'A'+i]];

        }

        return titleArray;

    }

    12 滚动到最后一行

    QQ会话中总是希望添加一行就向上滚动总是显示最新的消息

    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:_messageArray.count-1 inSection:0];记录最下面的行的位置

    [self.QQTalkTableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];滚到哪里

    13 有新数据时加载数据

    [self.myTalkTableView reloadData];

    有很多种刷新,视具体情况而定

  • 相关阅读:
    Adding and Deploying Solutions with PowerShell in SharePoint 2010
    List Schema
    sharepoint匿名实现的另一种方法
    SharePoint Tag Cloud
    Sharepoint Tags cloud
    Application Templates for Windows SharePoint Services 3.0
    asp.net弹出一个新页面时隐藏任务栏
    SQL提取数字,提取英文,提取中文,过滤重复字符方法
    TextBox中去掉边框、asp.net中实现行间距的代码
    asp.net中用LinkButton取到gridview中当前行的ID值
  • 原文地址:https://www.cnblogs.com/damonWq/p/5229662.html
Copyright © 2011-2022 走看看