1

The storyboard's dynamic prototype custom cell contains a system UIButton.

I set the button's title in 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];

Everything appears fine initially, until cells are scrolled off-screen. At that point, the reused cell's button title is no longer visible.

What I've checked:

  • The custom cell's button's properties, including its frame. Its placement and size are correct.

  • The button's state, titleForState and titleColorForState. It has a title and title color, but the title is no longer visible.

What I've tried:

  • Changing the button type from System to Custom. The button title still disappears.

  • Setting a background color for the button. The background shows up, with the right height and width for the title, but the title itself isn't visible.

Why does the button title disappear after scrolling?

Update:

  • If I set each cell's button title to the same string, the title is still visible after scrolling. The button title only apparently disappears when the new title differs from the previous title.

enter image description here

10
  • 1
    Can you put the entire code of your cellForRowAtIndexPath? Also, can you explain which part is not appearing in your screenshot?
    – Paul
    Mar 5, 2015 at 0:14
  • In the screenshot, the cell that comes into view at the bottom doesn't show "3 helpful votes" next to the "Galatians 3:26" label, even though I can examine that cell's button in the debugger, and see that its title is set. cellForRowAtIndexPath dequeues a reusable cell, and sets the cell's properties. There's no allocation going on there. I could post the code, but it's long, and I didn't want to obscure the button title issue.
    – user4151918
    Mar 5, 2015 at 2:30
  • Are you using prepareForReuse to reset the cell contents?
    – pbasdf
    Mar 8, 2015 at 23:57
  • @pbasdf No. All I do is set its title. (Haven't progressed to the point where I even hook up a target action.) Is there something specific I need to do for the button itself to be reused? This is my first time using a UIButton in a storyboard custom cell. The (storyboard) prototype button is obviously connected, allocated, and initialized properly by Apple's own code, but it's not obvious why that particular control has apparent reuse issues.
    – user4151918
    Mar 9, 2015 at 1:17
  • 1
    I think you should post your -tableView:cellForRowAtIndexPath: code. Otherwise there isn't much information about what might be the issue be. Mar 11, 2015 at 8:59

1 Answer 1

0
+50

I think you are mixing the cell styles without knowing.

No code for us to see...
so let me show one way to go about it...
by only one cell style and by using tags.

(you can use multiple cell styles but have different
dequeueReusableCellWithIdentifier for each cell style)

I'm using a Dynamic Prototype cell
The cell style is set to custom
I tried to have the same layout like yours

I gave the button in question a tag of 201

I'm also using the helpfulVotes label to display the row.
In IB, I gave it a tag of 202

Everything is created in the storyboard.
I'm just using references in cellForRowAtIndexPath:

I also created an @IBAction doSomething,
to prove we can keep track of which button is clicked.

image 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 version, which is like slang 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
}
3
  • Thanks for trying to solve the problem :) Definitely not using more than one cell style. I only have 1 custom dynamic prototype cell, which I dequeue with BIBLETableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];. I have subclassed UITableViewCell and connected up the button via Interface Builder, so I didn't need to use tags. The button title will appear in every cell as long as I don't try to change the button title from cell to cell.
    – user4151918
    Mar 14, 2015 at 4:02
  • Everything u r saying sounds good, you must have a typo. Can we see the code? At any rate, I removed the tags, created a BIBLETableViewCell class with @property (weak, nonatomic) IBOutlet UIButton *detailButton; Now using your code from above, I can call [cell.detailButton setTitle:votes forState:UIControlStateNormal]; And it works fine. You may have a problem with the constraints - they always mess things up. If one pixel if off, it won't show the title.
    – Diversity
    Mar 14, 2015 at 13:50
  • Thanks for showing that it works in your sample code. It looks like Auto Layout is placing the button frame in the right location. (I can see that by temporarily setting a background color for the button.) But there's no visible title, only a background. I think you're right about the constraints affecting the button title, as the title only disappears when its width (length) has changed.
    – user4151918
    Mar 14, 2015 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.