gpt4 book ai didi

ios - 对自定义单元格使用 dequeueReusableCellWithIdentifier

转载 作者:可可西里 更新时间:2023-11-01 04:44:16 25 4
gpt4 key购买 nike

假设我有

- (UITableViewCell*)tableView:(UITableView*) cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *cellID = @"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
return cell;
}

UILabel * nameLabel = [[UILabel alloc] initWithFrame: CGRectMake( 0, 15, box.size.width, 19.0f)];
nameLabel.text = name;
[nameLabel setTextColor: [UIColor colorWithRed: 79.0f/255.0f green:79.0f/255.0f blue:79.0f/255.0f alpha:1.0f]];
[nameLabel setFont: [UIFont fontWithName: @"HelveticaNeue-Bold" size: 18.0f]];
[nameLabel setBackgroundColor: [UIColor clearColor]];
nameLabel.textAlignment = NSTextAlignmentCenter;
[cell addSubview: nameLabel];
}

这是要做什么?

如果单元格不为零,假设您在第 5 行,它会返回第 5 行的单元格以及确切的文本标签等吗?

基本上,我的问题是,如果您有带有标签、 ImageView 等的自定义单元格。您如何将 cellForRowAtIndexPathdequeueReusableCellWithIdentifier 一起使用?

最佳答案

您尝试使一个单元格出列。如果尝试失败(单元格为零),则创建一个单元格并将其配置为 View (而不是 View 内的数据)。之后,您可以使用更改单元格之间的任何数据或设置来填充 View 。此外,您应该将任何自定义 View 添加到单元格的 contentView,而不是单元格本身。

#define NAME_LABEL_TAG 1234

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel * nameLabel = [[UILabel alloc] initWithFrame: CGRectMake( 0, 15, box.size.width, 19.0f)];
nameLabel.tag = NAME_LABEL_TAG;
[nameLabel setTextColor: [UIColor colorWithRed: 79.0f/255.0f green:79.0f/255.0f blue:79.0f/255.0f alpha:1.0f]];
[nameLabel setFont: [UIFont fontWithName: @"HelveticaNeue-Bold" size: 18.0f]];
[nameLabel setBackgroundColor: [UIColor clearColor]];
nameLabel.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview: nameLabel];
}

// Populate views with data and retrieve data for "name" variable
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:NAME_LABEL_TAG];
nameLabel.text = name;

// Return fully configured and populated cell
return cell;
}

如果您有一个复杂的单元格,在 Interface Builder 和子类 UITableViewCell 中创建它通常更容易,因此您可以拥有引用标签、按钮等的自定义属性。

关于ios - 对自定义单元格使用 dequeueReusableCellWithIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17687430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com