簡體   English   中英

在UITableViewCell中更改UILabel的文本顏色

[英]Changing the text color of UILabel in UITableViewCell

單擊按鈕時,需要更改UITableViewCell包含的UILabel的文本顏色。 我創建了一個UIColor object ,將其設置為單擊按鈕時指定的顏色,並調用了UITableView reloadData方法,即使顏色沒有變化。 我應該如何實施呢?

我假設您要更改表中所有行的顏色。 (如果只想更改一行,則需要使用它的indexPath )。

創建一個BOOL變量來跟蹤顏色狀態。 在您的按鈕單擊例程中,適當地設置此值並執行[tableView reloadData]

cellForRowAtIndexPath您需要每次檢查變量並設置兩種狀態的顏色。 這兩種方法都非常重要,否則滾動時單元格的狀態將不可預測。

要更改UITableViewCell的文本顏色,您必須實現UITableViewDataSource委托函數

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath(NSINdexPath*)indexPath

在那兒,你得到的細胞


UITableViewCell *tableViewcell = [self.tableView dequeueReusableCellWithIdentifier: @"Cell"];

 if (tableViewCell == nil) 
{
    tableViewCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
}

在這里,您可以更改文本的顏色。

順便說一句:您應該接受對問題的一些答案-這將增加您獲得未來問題的答案的機會...

很好,在將自定義UILabel添加到contentView的同時,為UILabel設置標簽。

UILabel *cl = nil;
cl = (UILabel*)[cell.contentView viewWithTag:2000];
if( !cl )
{
    cl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,30)];
    cl.tag = 2000;
    [cell.contentView addSubview:cl];
}
if( beforeButtonPressed )
cl.textColor = [UIColor blackColor];
else if ( afterButtonPressed )
cl.textColor = [UIColor blueColor];

我想以上方式將是您需要的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM