gpt4 book ai didi

uitableview - 具有自定义单元格的表格 View (以编程方式)

转载 作者:行者123 更新时间:2023-12-04 16:13:16 25 4
gpt4 key购买 nike

到目前为止,我曾经根据需要创建自定义 Nib 来制作我的单元格,但是这次,单元格的高度会从一个改变为另一个,因此我无法创建固定大小的单元格的 Nib 。

因此,我决定以编程方式创建它。

// Customize the appearance of table view cells.
- (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];
UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
[pseudoAndDate setTag:1];
[cell addSubview:pseudoAndDate];
[pseudoAndDate release];
}

CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

UILabel *label = (UILabel *)[cell viewWithTag:1];
[label setText:[NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date]];

return cell;
}

还是..我在这里想念什么吗?原因到目前为止,它似乎不起作用;)

谢谢,

哥提

最佳答案

为什么在不需要时创建标签?使用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];
}

CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

cell.textLabel.text = [NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date];

return cell;
}

关于uitableview - 具有自定义单元格的表格 View (以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2529148/

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