UITableView基本属性详解

该主题主要讲述uitableview的基本属性的设置:

//创建UITableView

    //设置table的类型:UITableViewStylePlain为普通列表模式,UITableViewStyleGrouped为分组模式
    /*
     typedef NS_ENUM(NSInteger, UITableViewStyle) {
     UITableViewStylePlain,                  // regular table view
     UITableViewStyleGrouped                 // preferences style table view
     };
     */
    _table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 200, 200) style:UITableViewStylePlain]autorelease];

  

    //设置table的水平指示,该属性继承自UIScrollView

    _table.showsHorizontalScrollIndicator = YES;
    //设置table的垂直指示,该属性继承自UIScrollView

    _table.showsVerticalScrollIndicator = YES;


    //设置table的cell是否允许被选择,当为false所有有关cell被选择的回调实效

    _table.allowsSelection = YES;


    //设置table的背景为一个自定义的view
    UIImageView* bg = [[UIImageView alloc]initWithFrame:_table.frame];
    bg.image = [UIImage imageNamed:@"bg@2x.png"];
    _table.backgroundView = bg;

    [bg release];


    //设置table的header和footer,自定义view
    _table.tableHeaderView = nil;

    _table.tableFooterView = nil;


    //设置table的row高度,也可以使用datasource的回调设置
    _table.rowHeight = 50.f;
    //设置table的section的header和footer的高度,也可以通过delegate的回调设置
    _table.sectionHeaderHeight = 20.f;

    _table.sectionFooterHeight = 20.f;


    //设置table的行与行间的分割线类型,颜色
    /*默认是UITableViewCellSeparatorStyleSingleLine,UITableViewCellSeparatorStyleNone是不显示分割线,UITableViewCellSeparatorStyleSingleLineEtched只能用于table的style是group分组模式
     
     typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
     UITableViewCellSeparatorStyleNone,
     UITableViewCellSeparatorStyleSingleLine,
     UITableViewCellSeparatorStyleSingleLineEtched   // This separator style is only supported for grouped style table views currently
     };
     */
    _table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    _table.separatorColor = [UIColor blueColor];


    //设置table的右边索引栏的字体颜色
    _table.sectionIndexColor = [UIColor yellowColor];
    //设置table的右边索引兰的背景颜色

    _table.sectionIndexTrackingBackgroundColor = [UIColor grayColor];


    //设置table的回调和datasource
    _table.delegate = self;
    _table.dataSource = self;

    [self.view addSubview:_table];


设置tableview常用回调和datasource

//datasource
//设置每个section的cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
//返回table的cell对象,只有当cell被滑动到table的可视范围内才被回调
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString* indentifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        /*默认cell的类型
         
         typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
         UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
         UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
         UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
         UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
         };
         
         */
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
        //设置自定义的cell被选中后的view
        UIView* selected = [[UIView alloc]initWithFrame:cell.frame];
        selected.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView =selected;
    }
    cell.textLabel.text = @"text";
    //设置cell的被选中后style
    /*
     typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
     UITableViewCellSelectionStyleNone,
     UITableViewCellSelectionStyleBlue,
     UITableViewCellSelectionStyleGray
     };
     */
    //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   
    
    return cell;
}

//设置table的section个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}             // Default is 1 if not implemented

//设置设置每个section的header头部显示的文字内容,也可以使用自定义view的回调

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"textH";
}    // fixed font style. use custom view (UILabel) if you want something different
//设置设置每个section的footer尾部显示的文字内容,也可以使用自定义view的回调
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"textF"
}
//返回由section的所有title组成的index索引,在table右边的提示索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [NSArray arrayWithObjects:@"1",@"2",@"3", nil];

//delegate

//回调设置table每行cell的高度,会覆盖table.rowHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//回调设置table每个section的header的高度,会覆盖table.sectionHeaderHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
//回调设置table每个section的footer的高度,会覆盖table.sectionFooterHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
//
Section header & footer information. Views are preferred over title should you decide to provide both
//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}   // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}   // custom view for footer. will be adjusted to default or specified footer height



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值