Iphone 如何将子视图添加到cell.contentView?
A(新创建单元格时): 或B(每次找到单元格时): A还是B?谢谢 更新解决方案(感谢您的回答):Iphone 如何将子视图添加到cell.contentView?,iphone,uitableview,Iphone,Uitableview,A(新创建单元格时): 或B(每次找到单元格时): A还是B?谢谢 更新解决方案(感谢您的回答): 仅当在A中创建单元时才应添加子视图,但每次都应将值指定给标签等,如在B中 如果您创建自己的UITableViewCell子类并添加自己的子视图,那么这个解决方案将自然失效 像这样的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
仅当在A中创建单元时才应添加子视图,但每次都应将值指定给标签等,如在B中 如果您创建自己的UITableViewCell子类并添加自己的子视图,那么这个解决方案将自然失效 像这样的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
[label release];
}
// Get a reference to the label here
label.text = @"9:00am";
return cell;
}
通过这种方式,您可以获得只分配一次子视图的性能优势,并且可以根据需要在子视图上设置适当的属性 都是关于性能的。对于A,您可以将单元及其所有子视图重用;对于B,您可以仅重用原始单元,并在每次迭代中添加一个新的子视图,这比re:performance差得多
我说要么创建一个
UITableView
子类,要么使用解决方案a。@ohho,不要这样认为。我觉得一切都很好。B难道不继续给牢房贴标签吗?我认为在一些滚动之后,最终会出现多个单元格在其子视图中包含多个标签。如果我错了,请纠正我。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
label.text = @"9:00am";
[cell.contentView addSubview:label];
[label release];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
label.tag = 1;
[cell.contentView addSubview:label];
[label release];
} else {
label = (UILabel *) [cell viewWithTag:1];
}
label.text = [NSString stringWithFormat:@"%d", [indexPath row]];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
[label release];
}
// Get a reference to the label here
label.text = @"9:00am";
return cell;
}