简体   繁体   中英

UITableViewCell textLabel.text not showing in a single row

I have a plain table view that is laying out properly except that the textLabel on one of the rows is appearing as a black bar, ie like black text on a black background. If I select that row, the text shows up fine. But if I select another row, it disappears again.

I discovered that the row being blacked out was the one that I programatically selected during the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

My code looked something like this:

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.text = @"some text";

if (self.selectedRow == indexPath.row) cell.selected = YES;

I solved the problem by selecting the cell before setting the selectionStyle, ie moving the last line to the top.

I'm sure there must be a rational explanation but thought I'd contribute just in case someone else encounters something similar.

First, don't play with the selection at cellForRowAtIndexPath if you don't want to get a surprise like this one, the "selected" property of a cell might be changed manually (nothing at developers reference forbid to do that), and you'll to do it after [tableView reloadData] call if the cell needs to be selected just after the view appeared.

Second, placing the last line from the snippet leaves the cell not displayed as selected while isSelected will be YES - not good at all.

Third and the last, ".text =" after ".selected =" is what removes the black-on-black artifact, it has nothing to do with the selection style i believe. However, the cell will not be displayed as selected - and that is really bad.

Let me know if you need more details of how you can correctly select the cell programmatically.

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