TableViewCell_Style

来源:互联网 发布:informix怎么看端口号 编辑:程序博客网 时间:2024/06/05 18:23

关于TableView 。

其复用功能且不说,其便捷的Cell操作代理函数也且不说,这个地方着重介绍一下一个TableView中,那些需要关注UIView视图类容。

注意点简要记录如下:

1. 继承关系

@interface UITableView :UIScrollView

因此ScrollView 的那些代理函数在TableView中照用不误有时候会很有用,譬如ScrollTo_Offset


2. 附属携带披挂

@property UIView *tableHeaderView;// accessory view for above row content. default is nil. not to be confused with section header

@property  UIView *tableFooterView;// accessory view below content. default is nil. not to be confused with section footer

好好运用它们,会发现很神奇,很有用,当然,对应也有TableViewDelegate中的两个代理(它们是针对Section的,因此总在Section最上方,tableHeaderView总在Table最上方。)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height

注意使用以上代理的时候,需要代理返回 UIView.height.  


3.现在说道最重要的东西 UITableViewCell, 如果一个Table只有一种TableViewCell,那么自然不许多言,如果是需要一个TableView对应多种样式的Cell,直接上代码:

static NSString *cellIDImgvFull         =@"CNNewsCellTypeImgvFull";

static NSString *cellIDImgvSideR        =@"CNNewsCellTypeImgvSideR";

static NSString *cellIDImgvParts        =@"CNNewsCellTypeImgvParts";

static NSString *cellIDImgvNone         =@"CNNewsCellTypeImgvNone";


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


    CNNews *news =self.arrNewsModel[indexPath.row];

    CNNewsTableCell *cell;

    if ([news.imgTypeintegerValue] == 1) {

        cell = [tableView dequeueReusableCellWithIdentifier:cellIDImgvFull];

        if (!cell) {

            cell = [[CNNewsTableCellalloc] initWithStyle:CNNewsCellTypeImgvFullreuseIdentifier:cellIDImgvFull];

        }

    }elseif ([news.imgTypeintegerValue] == 2) {

        cell = [tableView dequeueReusableCellWithIdentifier:cellIDImgvSideR];

        if (!cell) {

            cell = [[CNNewsTableCellalloc] initWithStyle:CNNewsCellTypeImgvSideRreuseIdentifier:cellIDImgvSideR];

        }

    }elseif ([news.imgTypeintegerValue] == 3) {

        cell = [tableView dequeueReusableCellWithIdentifier:cellIDImgvParts];

        if (!cell) {

            cell = [[CNNewsTableCellalloc] initWithStyle:CNNewsCellTypeImgvPartsreuseIdentifier:cellIDImgvParts];

        }

    }elseif ([news.imgTypeintegerValue] == 0){

        cell = [tableView dequeueReusableCellWithIdentifier:cellIDImgvNone];

        if (!cell) {

            cell = [[CNNewsTableCellalloc] initWithStyle:CNNewsCellTypeImgvNonereuseIdentifier:cellIDImgvNone];

        }

    }

    cell.news = news;

    

    return cell;

}

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

    CNNews *news =self.arrNewsModel[indexPath.row];

    CGFloat cellFit_H = [CNNewsTableCellcellHeightWithModel:news];

    return cellFit_H;

}


还有一种方法,其实- (UITableViewCell *)代理中是一种比较容易理解的写法,还有一种是先用TableView 注册了各个样式的Cell,然后分别使用代理函数返回使用,核心代码如下:

        [self.tableViewregisterClass:[CNNewsTableCellclass] forCellReuseIdentifier:cellIDImgvFull];


- (UITableViewCell *) ……………………{ 

……

cell = [tableViewdequeueReusableCellWithIdentifier:cellIDImgvFullforIndexPath:indexPath];

return cell;

}


经过测试,发现如果使用IB 方法注册,那么第二种方法完全没有问题,更适合,倘若是喜欢使用纯代码构造视图,那么第一种方法不存在问题,第二种方法实惠让人感觉到一些意外的。

还没有去深究其中的缘由,只是猜想可能注册法则更合适与nib 等IB文件吧。相信逻辑合理的情况下,必定是那个一细节特色没有处理好所以得到差异的效果了。


总而言之,对于多种style 的Cell, 它们做区别的和兴就是 复用标示符号:identifier

A string identifying the cell object to be reused. This parameter must not be nil.

给每一个却别样式的Cell一个identifier,复用机制会根据这个identifier找寻复用样式。












0 0