Iphone 如果UITableView中没有可重用的单元格怎么办
如果我有大量的单元格,谁能告诉我管理Iphone 如果UITableView中没有可重用的单元格怎么办,iphone,ios,uitableview,Iphone,Ios,Uitableview,如果我有大量的单元格,谁能告诉我管理UITableView的正确方法是什么?每个单元格的界面取决于部分(每个单元格在其内容视图中保留不同的UI元素)。我不想使用可重复使用的单元,因为它会弄乱重叠 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CustomCell alloc] initWithStyl
UITableView
的正确方法是什么?每个单元格的界面取决于部分(每个单元格在其内容视图中保留不同的UI元素)。我不想使用可重复使用的单元,因为它会弄乱重叠
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault nil] autorelease];
} else {
cell = nil;
[cell release];
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault nil] autorelease];
}
我在我的应用程序中使用可重复使用的单元。我使用的方法如下:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [yourArray objectAtIndex:row];
return cell;
}
它工作正常。我在我的应用程序中使用了可重复使用的单元。我使用的方法如下:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [yourArray objectAtIndex:row];
return cell;
}
它工作正常。不,您的代码不正确。首先,它甚至不会编译,因为
[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault nil]
不是有效的语法。其次是[细胞释放]代码>行没有效果(这是好的,因为如果它有,它将是错误的),但它的存在表明您还没有理解内存管理的概念(还没有)
第三,也是最重要的一点,您肯定应该使用表视图的单元重用,特别是如果您有一个大表的话。如果您有不同类型的单元,只需为它们使用不同的重用标识符,没有问题。然后,表视图将创建多个重用池,并始终返回您在dequeueReusableCellWithIdentifier:
中要求的单元格类型。不,您的代码不正确。首先,它甚至不会编译,因为[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault nil]
不是有效的语法。其次是[细胞释放]代码>行没有效果(这是好的,因为如果它有,它将是错误的),但它的存在表明您还没有理解内存管理的概念(还没有)
第三,也是最重要的一点,您肯定应该使用表视图的单元重用,特别是如果您有一个大表的话。如果您有不同类型的单元,只需为它们使用不同的重用标识符,没有问题。然后,表视图将创建多个重用池,并始终返回您在dequeueReusableCellWithIdentifier:
中要求的单元格类型。如果不使用ReuseIdentifier,您将快速耗尽内存,而表视图将缓慢滚动。您应该在此方法中更改单元格的内容,例如标题、图像等,但不应更改视图。所以,为您需要的每个部分创建cell的子类。设置其视图,并在此方法中设置内容
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
} else {
cell.title = [_cellTitles objectAtIndex:indexPath.row];
cell.image = [_cellImages objectAtIndex:indexPath.section];
}
如果没有ReuseIdentinterfier,内存会很快用完,而tableview的滚动速度会很慢。您应该在此方法中更改单元格的内容,例如标题、图像等,但不应更改视图。所以,为您需要的每个部分创建cell的子类。设置其视图,并在此方法中设置内容
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
} else {
cell.title = [_cellTitles objectAtIndex:indexPath.row];
cell.image = [_cellImages objectAtIndex:indexPath.section];
}
如果你有很多细胞,使用可重复使用的细胞是最好的方法。如果你有很多细胞,使用可重复使用的细胞是最好的方法。否则你会耗尽你的记忆。