gpt4 book ai didi

ios - 不工作 UITableViewCell 的方法

转载 作者:行者123 更新时间:2023-11-28 20:25:17 25 4
gpt4 key购买 nike

当我做简单的 HelloWorld 应用程序时,我遇到了问题

[self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]

,这个方法不行...我不知道为什么?请帮助

代码:

- (void)viewDidLoad
{
int i = 0;
[super viewDidLoad];
while (i < 10 ) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
i++;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"HelloWorld";
return cell;
}

应用应创建 10 个带有标签 @"HelloWorld"的单元格

最佳答案

你的做法是错误的。如果你想要 10 个单元格,你不应该尝试一次添加一个,你应该只在 numberOfRowsInSection 中返回 10,例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}

此外,单元格中不会显示任何内容,因为您已在 cellForRowAtIndexPath 中的单元格上调用 alloc/init。像这样修改您的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"HelloWorld";
return cell;
}

关于ios - 不工作 UITableViewCell 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266757/

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