iOS--UITableView用法

UITableView的部分属性:

    //注册UITableView dataSource代理,需遵守<UITableViewDataSource> 协议
    self.tableview.dataSource = self;

    //注册UITableView delegate代理,需遵守<UITableViewDelegate> 协议
    self.tableview.delegate = self;

    //设置UITableView的行高,这里是统一设置,如果自定义不同的cell高度,可以在代理方法中设置
    self.tableview.rowHeight = 50;

    //设置UITableView section Header高度
    self.tableview.sectionHeaderHeight = 10;

    //设置UITableView section Footer高度
    self.tableview.sectionFooterHeight = 10;

    //设置UITableView背景视图
    self.tableview.backgroundView = [[UIView alloc] init];

    //设置UITableView Cell是否可以点击
    self.tableview.allowsSelection = YES;
    
    //设置UITableView的HeaderView,显示在最顶部上面
    self.tableview.tableHeaderView = [[UIView alloc] init];

    //设置UITableView的FooterView,显示在底顶部下面
    self.tableview.tableFooterView = [[UIView alloc] init];

    //设置UITableView的分隔线的颜色
    self.tableview.separatorColor = [UIColor purpleColor];

    //设置UITableView的分隔线样式
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //获取UITableView的样式
    [self.tableview style];

    //获取UITableView有几个section
    [self.tableview numberOfSections];

    //获取UITableView有几个可见的Cell
    [self.tableview visibleCells];

UITableView的部分代理方法:

//UITableViewDataSource方法 @required
//设置UITableView此方法参数中Section中有多少行,设置返回值为NSInteger类型
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CarModel *model = self.carArray[section];
    return model.carsArray.count;
}

//设置UITableView此方法参数中indexPath中的cell内容,在此方法中可以设置Cell的内容或者自定义Cell
//设置返回值为UITableViewCell类型
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CarModel *model = self.carArray[indexPath.section];
    
    //一旦Cell过多,此方法会大量调用,创建的字符串指针在栈区会不断创建/释放,因此,使用static修饰后,无论调用多少次,只创建一次,这样可以提升效率,减少消耗。
    static NSString *indentifier = @"abcde";
    
    //创建Cell时,先从queue中根据indentifier寻找从屏幕滑出去的不用的Cell,如果有,就返回Cell,否则返回nil
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        //创建一个带有indentifier的新Cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        //Cell的类型,是一个枚举类型,自己可以点进去试一下,不同类型的Cell显示的样式有所不同
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = model.carsArray[indexPath.row];
    return  cell;
}

//UITableViewDataSource方法 @optional
//设置UITableView有几个Section,返回值是NSInterger类型
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
    return self.carArray.count;
}

//设置UITableView的行高,可以根据参数indexPath来自定义不同行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //额外小技巧:使用位运算判断奇偶,或者两奇一偶等等,效率极高。
    if ((indexPath.row & 1) == 0) {
        return 100;
    }
    return 100;
}
//设置UITableView Section Header(Section头部)显示的内容,返回值是NSString类型
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    CarModel *model = self.carArray[section];
    return model.title;
}

//设置UITableView Section Footer(Section尾部)显示的内容,返回值是NSString类型
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    CarModel *model = self.carArray[section];
    return model.describe;
}

//设置UITableView当前indexPath是否可以eidt
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return  YES;
}

//设置UITableView当前indexPath是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

//设置UITableView section title,需要给一个字符串数组,例如"ABCD...Z#"。。。
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
}

//设置UITableView移动一个行到另外一个行,一般用于修改位置
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
}

//点击UITableViewCell时调用的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

当数据变更时,想要刷新UITableViewCell的方法:

1.全部刷新

[self.tableview reloadData];

2.只刷新某个或某些个Cell

NSIndexPath *indexpath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
[self.tableview reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];

设置UITableView--tableFooterView的方法:

tableFooterView无论width设置多少,y设置多少,都不会起作用。只可设置x和hight

tableHeaderView的设置和特性一样。

//创建一个控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 414, 50);

//设置上面控件为tableFooterView
self.tableview.tableFooterView = button;

页面加载数据后,想要滑动到指定的cell

//设置想要移动的index位置
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.dataArray.count-10 inSection:0];

//让tableview调用以下方法,枚举类型参数可以参考苹果的说明,自己试一下也能看到效果
[self.tableview scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

猜你喜欢

转载自blog.csdn.net/JustinZYP/article/details/124317185