2016-12-07 45 views
0

我有包含視頻節目列表的tableview。當我點擊一個節目時,該節目將在該桌面視圖上方的播放器視圖中播放。它看起來像這樣。以下事件發生時更改UITableViewCell中的文本顏色

enter image description here

我想要做什麼是我點擊一排我想該行中文字變成紅色的顏色後。我的問題是它不能立即變紅,但我必須滾動該行的桌面才能離開屏幕並再次回到屏幕上,然後該選定行中的文本將變爲紅色。我知道這是tableview cellForRowAtIndexPath的行爲。所以我在表格中添加[self.tableView reloadData]didSelectRowAtIndexPath但它不起作用。正如我所說,我必須滾動選定的滾動屏幕,然後返回到屏幕上,以改變它的顏色。我該如何解決這個問題?

下面是相關的代碼。

- (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]; 
} 
+0

從我注意到你沒有通過檢查設置你的顏色它的indexPath,但有一些其他的值你定義和更新。更新這些ID的代碼是否可以在tableView didSelectRowAtIndexPath之後運行? –

回答

1

最簡單的事情就是提取將單元格配置爲自己的方法的邏輯。然後你的cellForRowAtIndexPath和didSelect方法可以通過撥打同樣的方法

- (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; 
} 
0

你「self.tableView」應該是相同的「的tableView」在「didSelectRowAtIndexPath方法」。

0

您可以使用 - (空)的tableView:(UITableView的*)的tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath委託函數是這樣的:

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

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


} 
相關問題