2016-07-06 32 views
0

我想啓用「暗模式」功能,我的應用程序,在我的tableview細胞,我改變了textcolors像這樣如何編程修改UITableViewCells文本顏色在運行時

if ([FS isDarkModeEnabled]) { 
     cell.CATEGORY.textColor = [UIColor whiteColor]; 
     cell.ORIGINUSER.textColor = [UIColor whiteColor]; 
     cell.NUMFILES.textColor = [UIColor whiteColor]; 
     cell.NUMTASKS.textColor = [UIColor whiteColor]; 
     cell.SHARECOUNT.textColor = [UIColor whiteColor]; 
    } 

我想我d放回了上面的代碼進行循環,但

FSCategoriesTVCCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath]; 
for (UILabel *lbl in [cell2.contentView subviews]) { 
    [lbl setTextColor:[UIColor greenColor]]; 
} 

但我無法得到它的工作。

所有上面的代碼中- tableView:cellForRowAtIndexPath:

編輯完成添加一些澄清

我知道如何手動更改文本顏色,我想要做的是循環遍歷單元格的內容和改變顏色通過for循環,而不是通過聲明每個標籤並單獨更改每個標籤。

或者,在更短的話,我想for循環,不要做cell.Label1.textColor = blah

+0

,您需要做的僅僅細胞.textLabel.textColor in cellForRow方法。 –

+0

您使用的是默認的單元格textLabel還是您在單元格中聲明瞭任何標籤? –

+0

@TejaNandamuri ive subclassed'UItableviewcell'包括7個標籤和一個imageview – highboi

回答

3

當黑暗模式是由用戶啓用,你必須告知tableview中,它應該更新細胞。要做到這一點,只需調用tableView的[tableView reloadData]方法即可。

+0

我正在使用NSNotifications來處理,請參閱我的更新。 – highboi

0

選項1:當查看加載

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.textColor = [UIColor blueColor]; 
    cell.textLabel.text = @"highboi"; 
    return cell; 
} 

選項2:當點擊,或者如果你想改變表格單元格中的文本標籤的顏色選擇的tableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor redColor]; 
} 
+0

OP是否表示要在選擇時更改顏色?最好在cellForRowAtIndexPath中設置條件邏輯中的標籤的textColor,並在應用模式更改時重新加載,如@Keneth所示 – danh

相關問題