美文网首页iOS
tableViewCell相关方法

tableViewCell相关方法

作者: 清都 | 来源:发表于2018-03-07 09:46 被阅读7次

    用XIB创建tableViewCell的方法

    创建方法

    1. 创建附带xib的UITableViewCell,在xib上设置自己想要的布局,并将其作为属性通过xib拖到cell的.m文档;

    2. 设置重用标志:在xib的identifier选项上填写想要使用的重用标志(此处使用xibCell作为重用标志),该标志需要和创建UITableView处的【通过nib注册cell对象】及协议方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里的重用标识皆为同一个;

    3. 在创建UITableView处使用如下代码,通过nib注册附带xib的cell:

    //cellID为重用标识,采用static修饰,本文档处为xibCell,根据喜好自行设置
    
    /** 加载xib 返回nib对象*/
    UINib * cellNib = [UINib nibWithNibName:@"NotiTableViewCell" bundle:nil];
    /** 通过nib对象,注册cell,设置重用*/
    [_myTableView registerNib:cellNib forCellReuseIdentifier:cellID];
    

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath设置cell的内容:

    NotiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    NSString * tmpString = [NSString stringWithFormat:@"%ld",indexPath.row];
    UIImage * theImage = [UIImage imageNamed:tmpString];
    //在cell的.h文档自行设置方法,用于更改cell上控件的属性
    [cell BSGSetImage:theImage leftLabel:tmpString rightLabel:tmpString];
    return cell;
    

    ps: 如果创建的自定义cell没有xib则自行新建后,需在cell的custom class-class里设置关联的cell类名;重用标志仍然是必须的。

    代码示例:

    GitHub-XIBCell

    创建cell的另一种方法

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath内部:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    cell相关设置

    cell点击不变色

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    设置cell的阴影

    
    UIView *shadowView = [[UIView alloc] init];
        shadowView.layer.shadowColor = [UIColor grayColor].CGColor;
        shadowView.layer.shadowOffset = CGSizeMake(0, 6);
        shadowView.layer.shadowOpacity = 0.25;
        shadowView.layer.shadowRadius = 4.0;
        shadowView.layer.cornerRadius = 8.0;
        shadowView.clipsToBounds = NO;
        [self.contentView addSubview:shadowView];
    

    相关文章

      网友评论

        本文标题:tableViewCell相关方法

        本文链接:https://www.haomeiwen.com/subject/buodfftx.html