• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS--UITableView用法

武飞扬头像
JustinCan
帮助1

UITableView的部分属性:

  1.  
    //注册UITableView dataSource代理,需遵守<UITableViewDataSource> 协议
  2.  
    self.tableview.dataSource = self;
  3.  
     
  4.  
    //注册UITableView delegate代理,需遵守<UITableViewDelegate> 协议
  5.  
    self.tableview.delegate = self;
  6.  
     
  7.  
    //设置UITableView的行高,这里是统一设置,如果自定义不同的cell高度,可以在代理方法中设置
  8.  
    self.tableview.rowHeight = 50;
  9.  
     
  10.  
    //设置UITableView section Header高度
  11.  
    self.tableview.sectionHeaderHeight = 10;
  12.  
     
  13.  
    //设置UITableView section Footer高度
  14.  
    self.tableview.sectionFooterHeight = 10;
  15.  
     
  16.  
    //设置UITableView背景视图
  17.  
    self.tableview.backgroundView = [[UIView alloc] init];
  18.  
     
  19.  
    //设置UITableView Cell是否可以点击
  20.  
    self.tableview.allowsSelection = YES;
  21.  
     
  22.  
    //设置UITableView的HeaderView,显示在最顶部上面
  23.  
    self.tableview.tableHeaderView = [[UIView alloc] init];
  24.  
     
  25.  
    //设置UITableView的FooterView,显示在底顶部下面
  26.  
    self.tableview.tableFooterView = [[UIView alloc] init];
  27.  
     
  28.  
    //设置UITableView的分隔线的颜色
  29.  
    self.tableview.separatorColor = [UIColor purpleColor];
  30.  
     
  31.  
    //设置UITableView的分隔线样式
  32.  
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
  33.  
     
  34.  
    //获取UITableView的样式
  35.  
    [self.tableview style];
  36.  
     
  37.  
    //获取UITableView有几个section
  38.  
    [self.tableview numberOfSections];
  39.  
     
  40.  
    //获取UITableView有几个可见的Cell
  41.  
    [self.tableview visibleCells];
学新通

UITableView的部分代理方法:

  1.  
    //UITableViewDataSource方法 @required
  2.  
    //设置UITableView此方法参数中Section中有多少行,设置返回值为NSInteger类型
  3.  
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  4.  
    CarModel *model = self.carArray[section];
  5.  
    return model.carsArray.count;
  6.  
    }
  7.  
     
  8.  
    //设置UITableView此方法参数中indexPath中的cell内容,在此方法中可以设置Cell的内容或者自定义Cell
  9.  
    //设置返回值为UITableViewCell类型
  10.  
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  11.  
    CarModel *model = self.carArray[indexPath.section];
  12.  
     
  13.  
    //一旦Cell过多,此方法会大量调用,创建的字符串指针在栈区会不断创建/释放,因此,使用static修饰后,无论调用多少次,只创建一次,这样可以提升效率,减少消耗。
  14.  
    static NSString *indentifier = @"abcde";
  15.  
     
  16.  
    //创建Cell时,先从queue中根据indentifier寻找从屏幕滑出去的不用的Cell,如果有,就返回Cell,否则返回nil
  17.  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
  18.  
    if (!cell) {
  19.  
    //创建一个带有indentifier的新Cell
  20.  
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
  21.  
    //Cell的类型,是一个枚举类型,自己可以点进去试一下,不同类型的Cell显示的样式有所不同
  22.  
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  23.  
    }
  24.  
    cell.textLabel.text = model.carsArray[indexPath.row];
  25.  
    return cell;
  26.  
    }
  27.  
     
  28.  
    //UITableViewDataSource方法 @optional
  29.  
    //设置UITableView有几个Section,返回值是NSInterger类型
  30.  
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
  31.  
    return self.carArray.count;
  32.  
    }
  33.  
     
  34.  
    //设置UITableView的行高,可以根据参数indexPath来自定义不同行的高度
  35.  
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  36.  
    //额外小技巧:使用位运算判断奇偶,或者两奇一偶等等,效率极高。
  37.  
    if ((indexPath.row & 1) == 0) {
  38.  
    return 100;
  39.  
    }
  40.  
    return 100;
  41.  
    }
  42.  
    //设置UITableView Section Header(Section头部)显示的内容,返回值是NSString类型
  43.  
    - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  44.  
    CarModel *model = self.carArray[section];
  45.  
    return model.title;
  46.  
    }
  47.  
     
  48.  
    //设置UITableView Section Footer(Section尾部)显示的内容,返回值是NSString类型
  49.  
    - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
  50.  
    CarModel *model = self.carArray[section];
  51.  
    return model.describe;
  52.  
    }
  53.  
     
  54.  
    //设置UITableView当前indexPath是否可以eidt
  55.  
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  56.  
    return YES;
  57.  
    }
  58.  
     
  59.  
    //设置UITableView当前indexPath是否可以移动
  60.  
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
  61.  
     
  62.  
    }
  63.  
     
  64.  
    //设置UITableView section title,需要给一个字符串数组,例如"ABCD...Z#"。。。
  65.  
    - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  66.  
     
  67.  
    }
  68.  
     
  69.  
    //设置UITableView移动一个行到另外一个行,一般用于修改位置
  70.  
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
  71.  
     
  72.  
    }
  73.  
     
  74.  
    //点击UITableViewCell时调用的方法
  75.  
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  76.  
     
  77.  
    }
学新通

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

1.全部刷新

[self.tableview reloadData];

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

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

设置UITableView--tableFooterView的方法:

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

tableHeaderView的设置和特性一样。

  1.  
    //创建一个控件
  2.  
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  3.  
    button.backgroundColor = [UIColor redColor];
  4.  
    button.frame = CGRectMake(0, 0, 414, 50);
  5.  
     
  6.  
    //设置上面控件为tableFooterView
  7.  
    self.tableview.tableFooterView = button;

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

  1.  
    //设置想要移动的index位置
  2.  
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.dataArray.count-10 inSection:0];
  3.  
     
  4.  
    //让tableview调用以下方法,枚举类型参数可以参考苹果的说明,自己试一下也能看到效果
  5.  
    [self.tableview scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbeiek
系列文章
更多 icon
同类精品
更多 icon
继续加载