SanitLee
SanitLee

Reputation: 1253

Change text color in UITableViewCell following event happened

I have tableview that contains video program lists. When I tap on a program, that program will be played on player view above that tableview. It looks like this.

enter image description here

What I want to do is after I tap a row I want text in that row to turn Red color. My problem is it cannot turn Red instantly but I have to scroll the tableview for that row to go off screen and come back on screen again then the text in that selected row will become Red. I know this is behavior of tableview cellForRowAtIndexPath. So I add [self.tableView reloadData] in tableview didSelectRowAtIndexPath but it doesn't work. As I said I have to scroll that selected roll off screen then come back on screen in order to change its color. How can I resolve that problem?

Here below is related code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"LiveTableViewCell";

   LiveTableViewCell *cell = (LiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   //...... (more stuff here but not related)

   //Color - for one selected/playing
   if (programList.programId == self.liveData.programId ) {
      [str1 addAttribute:NSStrikethroughColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
      [str1 addAttribute:NSForegroundColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
    }

    cell.title.attributedText = str1;

    return cell;
}

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

   //...... (more stuff here but not related)
   [self.tableView reloadData];  
}

Upvotes: 0

Views: 72

Answers (3)

Sagar Snehi
Sagar Snehi

Reputation: 398

You can use - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath delegate function like this:

(void)tableView:(UITableView *)tableView 
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{

  LiveTableViewCell *cell = (LiveTableViewCell *)[tableView    cellForRowAtIndexPath:indexPath];
  cell.title.textColor = UIColor.redColor;


}

Upvotes: 0

留什么白
留什么白

Reputation: 67

Your 'self.tableView' should be same with the 'tableView' in 'didSelectRowAtIndexPath'.

Upvotes: 0

Paul.s
Paul.s

Reputation: 38728

The easiest thing to do is to extract the logic that configures the cell into it's own method. Then your cellForRowAtIndexPath and didSelect methods can call through to the same method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"LiveTableViewCell";

   LiveTableViewCell *cell = (LiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   //...... (more stuff here but not related)
   [self configureCell:cell withProgramList:programList];

    return cell;
}

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

   //...... (more stuff here but not related)

   LiveTableViewCell *cell = (id)[tableView cellForRowAtIndexPath:indexPath];
   ProgramList *programList = // get programList at indexpath
   [self configureCell:cell withProgramList:programList];
}

- (void)configureCell:(LiveTableViewCell *)cell withProgramList:(ProgramList *)programList {
   //Color - for one selected/playing
   if (programList.programId == self.liveData.programId ) {
      [str1 addAttribute:NSStrikethroughColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
      [str1 addAttribute:NSForegroundColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
    }

    cell.title.attributedText = str1;
}

Upvotes: 1

Related Questions