ohh*_*hho 25 iphone uitableview
A(当新创建单元格时):
- (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;
}
Run Code Online (Sandbox Code Playgroud)
或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;
}
Run Code Online (Sandbox Code Playgroud)
A还是B?谢谢!
更新解决方案(感谢答案):
- (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;
}
Run Code Online (Sandbox Code Playgroud)
Gar*_*ary 11
您应该只在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;
}
Run Code Online (Sandbox Code Playgroud)
这样,您只需分配子视图一次就可以获得性能优势,您可以根据需要在子视图中设置适当的属性.
Jac*_*kin 11
这完全取决于性能.使用A,您可以重复使用包含其所有子视图的单元格,使用B,您只重用原始单元格并在每次迭代时添加新的子视图,恕我直言不如A re:performance.
我说要么创建一个UITableView子类,要么使用解决方案A.
| 归档时间: |
|
| 查看次数: |
32132 次 |
| 最近记录: |