我正在尝试为我的表的选定行提供一个按钮。适合iPhone的UITableView的特定行上的不显示按钮
这里是我使用的示例代码:装载1时
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ControlRowIdentifier = @"ControlRowIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ControlRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ControlRowIdentifier] autorelease];
}
if ([indexPath row] > 5) {
UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height);
[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
[button setTitle:@"Tap" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
}
NSUInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
cell.textLabel.text = rowTitle;
return cell;
}
此代码工作绝对没问题。因此,根据逻辑,它显示所有大于5的行的“点击”按钮。
当我向上和向下滚动时会出现问题。一旦我这样做,它就开始把这个按钮放在任意一行。我不明白为什么它会这样做,如果有人可以提供一些提示,这将非常有帮助。
谢谢。
谢谢维克多!问题的确在于重新使用单元的标识符。一旦我使用了唯一标识符,问题就消失了! – user315603 2010-04-26 18:48:13
唯一标识符需要具有独特特征的单元格,在您的情况下是带有按钮的单元格和没有按钮的单元格。如果为每个单元生成唯一的标识符,他们将停止重用,这将影响效率 – Victor 2010-04-26 21:07:08