2012-06-29 24 views
3

在我的代碼的一部分,我叫不能設置單元格中的α的cellForRowAtIndexPath

UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 

得到一些細胞,然後我改變自己的阿爾法至0.35。這可行,細胞看起來褪色。

但是,當我滾動tableview時,移出窗口然後移回的單元格不再褪色。即使在cellForRowAtIndexPath中,每個禁用的行的alpha值都設置爲.35,但會發生這種情況。

我登錄

NSLog(@"%f", cell.alpha); 

,並返回0.35

但是權的cellForRowAtIndexPath返回細胞之前,細胞不滾動後褪色?在顯示之前,它看起來像是阿爾法神奇地變回到1。

以下是我執行的cellForRowAtIndexPath在我科類

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 

if ([self disabled]) 
{ 
    cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:@"CellDisabled" style:UITableViewCellStyleDefault]; 
    cell.alpha = .35; 
} 
else 
{ 
    cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:@"CellEnabled" style:UITableViewCellStyleDefault]; 
    cell.alpha = 1; 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

//if-else block that only sets labels is here omitted 

if (cell.textLabel.text == [[cs objectAtIndex:[self c]] type]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

NSLog(@"%f", cell.alpha); 

return cell; 

}

而且,協調這些部分得到的細胞從他們身上,像這樣的視圖控制器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Alpha: %f", [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath].alpha); 
    return [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath]; 
} 

(和NSLog記錄適當的阿爾法值)

+2

您可以發佈您的cellForRowAtIndexPath實現嗎? – Dima

回答

2

此問題可能是由於單元格在表格視圖中被重用。有一個更好的方法來做到這一點:

你應該在你的視圖控制器中創建一個NSMutableDictionary,併爲應該淡化的索引設置鍵值。

然後,在cellForRowAtIndexPath中,在設置單元格時,根據索引是否存在爲字典中的鍵來設置alpha。

此解決方案將工作沒有任何奇怪的行爲。

+0

即使我給每個單元格一個唯一的標識符,這仍然會發生。 –

+1

另外,如果我使用這個: [[cell textLabel] setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.35f]]; [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.35f]]; 代替改變阿爾法,它的作品,但我不能動畫。 –

+0

在您發佈的代碼中,您正在檢查'[自我禁用]'。這代表什麼?你正在檢查的不是單元格的功能,而是你的視圖控制器的功能。 – Dima

12

添加所有子視圖單元格的內容查看並使用下面的字符串中-cellForRowAtIndexPath

cell.contentView.alpha = {needed_value};

會爲你的作品。

+3

真的很重要的細節,請務必將所有的子視圖添加到contentView的子視圖,而不是單元格。我花了很長時間試圖弄清楚爲什麼我的子視圖在單元格時沒有改變。 – loadedion

1

請確保您也設置了cell.contentView的alpha值。同時在tableView:willDisplayCell:forRowAtIndexPath:中設置alpha,因爲它可能不適用於iOS 7

相關問題