2012-08-24 39 views
0

我正在嘗試向表格視圖中的每個單元格添加一個自定義單選按鈕。當我第一次查看錶格視圖時,我看不到任何單選按鈕。但是當我向下滾動時,可以看到當視圖第一次加載時看到的初始單元格下方的每個單元格上的單選按鈕。而一旦沒有單選按鈕的單元出現在視圖中,我又回過頭去查看那個單元,就會出現單選按鈕。我向下滾動頁面一次後會出現表視圖按鈕

這裏是我的這一個方法的代碼:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
_radioBtn.frame = CGRectMake(275, 3,36,36); 
_radioBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[_radioBtn setImage:[UIImage imageNamed:@"Radio-Btn.png"] forState:UIControlStateNormal]; 
[cell.contentView addSubview:_radioBtn]; 

NSString *cellValue = [_arrayRelat objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 
cell.textLabel.textColor = [UIColor colorWithRed:(88.0/255.0) green:(88.0/255.0) blue:(89.0/255.0) alpha:1]; 

return cell; 
} 

讓我知道,如果你不明白的問題。

+0

什麼是_radioBtn? – Bot

+0

我不明白,你如何得到一個沒有單選按鈕的單元格!您正在向所有單元格添加單選按鈕。沒有條件添加單選按鈕。 –

回答

1

你正在用每個新單元覆蓋_radioBtn! 你應該爲每個小區創建的UIButton的一個新的實例如:

UIButton *radioBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
radioBtn.frame = CGRectMake(275, 3,36,36); 
[radioBtn setImage:[UIImage imageNamed:@"Radio-Btn.png"] forState:UIControlStateNormal]; 
[cell.contentView addSubview:radioBtn]; 

,你應該把你的if (cell == nil)塊內只能創建每個單元一次。

+0

對不起,我不能投票你的答案,我剛剛得到了這個問題的投票,所以我的分數小於15 .. – KKendall

相關問題