简体   繁体   中英

Can't set UIButton highlighted in UITableViewCell

I would like to set my UIButton highlighted when the user taps a cell, it looked pretty easy, but unfortunately it doesn't work somewhy. I have a custom class for the cell and I'm trying to set it highlighted in the didSelectRowAtIndexPath: method, but when I tap it nothing happens despite [self showActionSheet]; has been called every time. I also tried to do it in the MDTableViewCell.m 's setSelected:animated: method, in this case it highlights every cell's button right after I load the table view. Am I missed something or I do in a wrong place?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    MDTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"bookCell"];
    [cell.Buttn setSelected:YES];
    [cell.tButtn setHighlighted:YES];
    [self showActionSheet];

}

It doesn't work because you're dequeuing a new cell that never appears in your UI. If you're trying to get the cell that was tapped on, then use:

MDTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

This just answers your immediate question, but I'm not sure it's a good idea to highlight the button when you tap on a cell -- it should be highlighted if you tap on it (the button). Also, when you scroll, and the cell is reused, you will see a highlighted button appear in other rows. There are plenty of other answers out there on how to deal with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM