表格视图中的重复单元格 [英] Repeated cells in table view
问题描述
这是我的cellForRowAtIndexPath
.
this is my cellForRowAtIndexPath
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Maledetta"];
if (cell == nil) {
UIViewController *c;
if (!IS_IPAD) c = [[UIViewController alloc] initWithNibName:@"NewsRow" bundle:nil];
else c = [[UIViewController alloc] initWithNibName:@"NewsRow_ipad" bundle:nil];
cell = (NewsRowController*)c.view;
if ([titleArray count] > 0) {
[(NewsRowController*)cell setCellDataWithName:[titleArray objectAtIndex:indexPath.row]
andDate:[descArray objectAtIndex:indexPath.row]
day:[dayArray objectAtIndex:indexPath.row]
month:[monthArray objectAtIndex:indexPath.row]];
}
[c release];
}
return cell;
}
为什么只显示4行,之后又重复第4行直到第10行?
Why it show me only 4 row and after that repeat the first for fourth other time until ten???
+-----------------------+
| A
+-----------------------+
| B
+-----------------------+
| C
+-----------------------+
| D
+-----------------------+
| A (repeated)
+-----------------------+
| B (repeated)
+-----------------------+
| C (repeated)
+-----------------------+
| D (repeated)
+-----------------------+
| A (repeated)
+-----------------------+
| B (repeated)
+-----------------------+
Ah,[titleArray count]
等于10
.
kCustomCellID
是正确的.
Ah, [titleArray count]
is equal to 10
.
kCustomCellID
is correct.
谢谢.
A
推荐答案
如果在表的单元格队列中找不到该单元格,则只会填充该单元格.如果找到它,则不会用该indexPath.row值的内容覆盖它.
You're only populating the cell if it's not found in the table's cell queue. If it IS found, you're not overwriting it with the contents for that value of indexPath.row.
尝试以下方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Maledetta"];
if (cell == nil) {
UIViewController *c;
if (!IS_IPAD) c = [[UIViewController alloc] initWithNibName:@"NewsRow" bundle:nil];
else c = [[UIViewController alloc] initWithNibName:@"NewsRow_ipad" bundle:nil];
cell = (NewsRowController*)c.view;
[c release];
}
if ([titleArray count] > 0) {
[(NewsRowController*)cell setCellDataWithName:[titleArray objectAtIndex:indexPath.row]
andDate:[descArray objectAtIndex:indexPath.row]
day:[dayArray objectAtIndex:indexPath.row]
month:[monthArray objectAtIndex:indexPath.row]];
}
return cell;
}
此外,[titleArray count]的检查可能是多余的.您正在使用它来给出此表的部分中的单元格数量,对吗?如果那是零,它甚至都不会到达这里.
Also, the check of [titleArray count] is probably redundant. You're using that to give the number of cells in section for this table, right? If that's zero, it won't even get here.
这篇关于表格视图中的重复单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!