UItableView

来源:互联网 发布:费米估算法的原理 编辑:程序博客网 时间:2024/05/18 14:25

http://blog.sina.com.cn/s/blog_7b9d64af01019x3t. html


UITableView 应用(三)UITableViewDelegate 方法总结

 

1.定义每个UITableView中的cell的行高

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

{

    // indexPath.section,根据分组进行更精确的配置

    return 90.0;

}


2.设置UITableView每个分组的Header的Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [_arrayType objectAtIndex:section];

    

}


3.设置UITableView分组Header的高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30.0;

}


4.设置UITableView自定义的Header

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    // 自定义的Header

    ViewForCellHeader *headerView=[[[ViewForCellHeader alloc] init] autorelease];

    headerView.strSectionName=[_arrayType objectAtIndex:section];

    return  [headerView view];

}


注意:2与4是互斥的。

同理原理,我们分别也对Footer进行设置。

5.设置UITableView每个分组的Footer的Title

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return [_arrayType objectAtIndex:section];

    

}


6.设置UITableView分组Footer的高

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    // indexPath.section,根据分组进行更精确的配置

    return 30.0;

}


7.设置UITableView自定义的Footer

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    // 自定义的Footer

    ViewForCellHeader *headerView=[[[ViewForCellHeader alloc] init] autorelease];

    headerView.strSectionName=[_arrayType objectAtIndex:section];

    return  [headerView view];

}


8.设置UITableView每个分组的Footer的Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [_arrayType objectAtIndex:section];

    

}



UITableView应用(二)UITableViewDataSource 方法总结

说明:

 

NSMutableDictionary *_dictData;// 所有数据

NSMutableArray *_arrayType;// 分组


1.返回分组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [_arrayType count];

}


2.根据分组,返回每个分组的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSString *curType=[_arrayType objectAtIndex:section];

    return [[_dictData objectForKey:curType] count]; 

}


3.根据分组,返回每个cell

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


    NSUInteger section = [indexPath section];

    NSUInteger row = [indexPath row];

    

    NSString *key=[_arrayType objectAtIndex:section];

    

    NSMutableArray *arrValues=[_dictData objectForKey:key];

    

    static NSString *CellIdentifier = @"WTVChannelCell";

 

    // 自定义cell

    WTVChannelCell *cell = (WTVChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil){

        cell = [[[WTVChannelCell alloc] initWithStyle:UITableViewCellSelectionStyleGray reuseIdentifier:CellIdentifier] autorelease];

        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"WTVChannelCell"owner:nil options:nil];

        cell = [array objectAtIndex:0];

    }

    

    cell.dictChannelData=[arrValues objectAtIndex:row];

    

    [cell refreshCellData];


    return cell;

    

}


uitabelViewCell 重用

dequeueResableCellWithIdentifier方法

//对table view的数据进行绑定,即填充cell,自动调用n次

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

   UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"FlipsideCellIdentifier"];

   if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero

reuseIdentifier:@"FlipsideCellIdentifier"] autorelease];

   }

    cell.text = [soundSignatures objectAtIndex:indexPath.row];

    return cell;

}

理解:

每一个UITableView里都维护着一个cell队列,当UITableView刚加载的时候,cell队列里是没有任何数据的。

dequeueResableCellWithIdentifier从字面上理解就是”出列可重用的cell",也就是根据一个标识identifier从cell队列里取出一个

UITableViewCell,当然了,如果cell队列里没有此标识的cell,调用此方法的结果就是返回nil。因此,在UITableView刚加载的时候,cell队列里没有可用的cell,所以必须通过语句

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

来创建对应CellIdentifier标识的UITableViewCell实例。

[ tableView:cellForRowAtIndexPath:方法主要是根据nsindex取得一个cell ]

而当UITableView在滚动的时候导致UITableViewCell滚出手机屏幕视图的时候,程序会将这一个UITalbeViewCell实例放入此UITableView所维护的cell队列中。当UITableview中有新的UITableViewCell需要展现在手机屏幕视图上时,就会调用tableView:cellForRowAtIndexPath:方法了。

因此我们可以知道以下几点:
1-重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)。
2-这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂。
3-设计的关键是实现cell和数据的完全分离

如果不想重用UITableViewCell实例,如在一个每一行都显示不同内容的UITableView实例时,我们可以用如下的方法:
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];
来重新定义标识。
这样每一行都有其对应的identifier,从cell队列里取出来只有两个结果:
1-cell队列里没有此identifier对应的UITableViewCell实例,返回nil
2-cell队列里有此identifier对应的UITableViewCell实例,而且不会有重用到其他不同行的cell的情况

来自:http://blog.sina.com.cn/s/blog_642e41c20100x5bj.html

0 0
原创粉丝点击