Iphone 刷新UITableView的好方法
我想知道有哪些好的、内存高效的方法可以重新加载对UITableView所做的所有更改。想象一下,如果您更改了便笺的日期/标题,Apple的原生便笺应用程序会刷新UITableView 当我将索引设置为显示内容的视图时,我会确保像这样重新加载索引数据Iphone 刷新UITableView的好方法,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,我想知道有哪些好的、内存高效的方法可以重新加载对UITableView所做的所有更改。想象一下,如果您更改了便笺的日期/标题,Apple的原生便笺应用程序会刷新UITableView 当我将索引设置为显示内容的视图时,我会确保像这样重新加载索引数据 [indexTable reloadData]; 现在唯一的问题是,如果以前使用过,并不是每个单元格都会被刷新,因为我在加载单元格的函数中有这样一个功能: //if (cell == nil) { // assign a label to them
[indexTable reloadData];
现在唯一的问题是,如果以前使用过,并不是每个单元格都会被刷新,因为我在加载单元格的函数中有这样一个功能:
//if (cell == nil) { // assign a label to them, e.g. the title of a note
//} else {
// label = (UILabel *) [cell viewWithTag:1];
//}
请参阅下面的完整代码。我现在真的很想知道label=(UILabel*)[cell view with tag:1]代码>位是什么?我所知道的是,如果用户输入了一个新的笔记标题(尽管我调用了indexTable reload!),它不会刷新我的数据
我是否会像在下面的代码中那样将其忽略,从而遇到严重的问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//NSLog(@"checking if cell is nil");
//if (cell == nil) {
NSLog(@"cell=nil");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(10, 0, 200, 30);
label = [[UILabel alloc] initWithFrame:frame];
label.lineBreakMode = UILineBreakModeTailTruncation;
label.tag = 1;
label.text = [[indexContent objectAtIndex:indexPath.row] itemTitle];
[cell.contentView addSubview:label];
[label release];
//} else {
// label = (UILabel *) [cell viewWithTag:1];
//}
return cell;
}
任何解释都将不胜感激 每当滚动或重新加载tableview时,上述代码将启动并分配每个单元格的内存。你应该使用
if (cell == nil) {
NSLog(@"cell=nil");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
因此,它不会在每次向下或向上滚动tableview时分配和创建新单元格。重新加载整个tableview的最佳方法是[tableview reloadData];据我所知
希望对您有所帮助。您应该检查的结果
[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
为nil
,虽然不这样做,但不会使你的应用程序崩溃,每次调用此方法时,你都会初始化/分配一个新单元格,最好尝试重用单元格(如果每次都分配/初始化一个新单元格,为什么要尝试重新启动单元格?
label=(UILabel*)[带标记的单元格视图:1]
不会进行任何刷新,它只是检索一个UILabel
,它是给定标记的单元格的子视图。在“如果”部分中,您可以分配/初始化一个新单元格,因为它是必需的,并配置其帧
、lineBreakMode
、和标记
属性,然后将其添加到单元格中
因此,为了让内容刷新(使用[tableView reloadData];
),在if/else序列之外提取UILabel
内容设置部分,并切断else部分:
if(cell == nil) {
//init/alloc and configure a new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(10, 0, 200, 30);
label = [[UILabel alloc] initWithFrame:frame];
label.lineBreakMode = UILineBreakModeTailTruncation;
label.tag = 1;
[cell.contentView addSubview:label];
[label release];
}
// you either retrieved the cell, or just created it
// retrieve the label and update the content
label = (UILabel *) [cell viewWithTag:1];
label.text = [[indexContent objectAtIndex:indexPath.row] itemTitle];
return cell;