滚动后自定义 UITableViewCell 按钮标题不再可见(重用)

Custom UITableViewCell button title no longer visible after scrolling (reuse)

故事板的动态原型自定义单元格包含一个系统 UIButton

我在 cellForRowAtIndexPath 中设置了按钮的标题:

NSInteger votesCount = verse.helpfulVotesCount.integerValue;
NSString *votes = [NSString stringWithFormat:@"​​%ld helpful vote%@", (unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];

[cell.detailButton setTitle:votes forState:UIControlStateNormal];

一开始一切都很好,直到滚动单元格 off-screen。那时,重复使用的单元格的按钮标题不再可见。

我检查过的内容:

我尝试过的:

为什么滚动后按钮标题消失了?

更新:

我认为你在不知情的情况下混合了单元格样式。

没有代码可供我们查看...
所以让我展示一种解决方法...
仅通过一种单元格样式和使用标签。

(您可以使用多个单元格样式,但每个单元格样式有不同的
dequeueReusableCellWithIdentifier)

我正在使用动态原型单元格
单元格样式设置为自定义
我试过和你一样的布局

我给有问题的 按钮 标记了 201

我也在使用 helpfulVotes label 来显示该行。
在IB中,我给它打了个tag 202

一切都在故事板中创建。
我只是在 cellForRowAtIndexPath:

中使用引用

我还创建了一个@IBAction doSomething,
以证明我们可以跟踪点击了哪个按钮。

图片link:http://tinypic.com/r/1yl1l4/8

- (IBAction)doSomething:(UIButton*)sender {
    NSLog(@"Button row clicked: %ld",(long)sender.titleLabel.tag);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    NSInteger votesCount = indexPath.row;
    NSString *votes =
    [NSString stringWithFormat:@"​​%ld helpful vote%@",
     (unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];

    UILabel *helpfulVotes = (UILabel *)[cell viewWithTag:202];
    [helpfulVotes setText:votes];

    UIButton *detailButton = (UIButton *)[cell viewWithTag:201];
    [detailButton setTitle:votes forState:UIControlStateNormal];
    //save indexPath.row inside this tag
    detailButton.titleLabel.tag = indexPath.row;

    return cell;
}

Swift 版本,类似于俚语 Objective-C:

    @IBAction func doSomething(sender: UIButton) {
    if let tag = sender.titleLabel?.tag {
        println("row:\(tag)")
    }
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
    // Configure the cell...
    var someTitle = cell.viewWithTag(202) as UILabel
    someTitle.text = "Row \(indexPath.row):00"

    var detailButton = cell.viewWithTag(201) as UIButton
    //save indexPath.row inside this tag
    detailButton.titleLabel?.tag = indexPath.row
    detailButton.setTitle("Button:\(indexPath.row)",
        forState: UIControlState.Normal)

    return cell
}