【发布时间】:2014-07-04 04:39:26
【问题描述】:
我是 Objective C 编程的新手,并且有一个带有自定义单元格的 UITableView 设置。我想让它让用户可以触摸一个按钮来添加另一个单元格,并且这个按钮只会出现在最后一个单元格中。目前,它没有出现。这是我正在使用的代码。我在自定义单元格中创建了按钮,并使用“setHidden:YES”将其隐藏在单元格本身中。我正在尝试“setHidden:NO”使按钮出现在 TableView 代码中,但它不起作用。我想也许这与重新加载单元有关,但我不确定我是否朝着正确的方向前进。我将不胜感激任何帮助,谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.addButton setTitle:(NSString *)indexPath forState:UIControlStateApplication];
[cell.textLabel setText:[NSString stringWithFormat:@"Row %i in Section %i", [indexPath row], [indexPath section]]];
NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
NSLog(@"Reached last cell");
[cell.addButton setHidden:NO];
if (lc == NO)
{[[self tableView] reloadData];
lc = YES;
}
}
return cell;
}
【问题讨论】:
-
为什么要在
cellForRowAtIndexpath中重新加载表? -
最好的方法是将你的按钮添加到表格页脚视图。
标签: ios objective-c uitableview uibutton