2011-12-26 84 views
0

我想動態添加一個UIButton到一個表格單元格 - 請參閱下面的要求。如何動態添加UIButton到UITableViewCell?

當用戶選擇一個小區,我簡單地調整該表單元格的高度在

-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath { 
     selIndPath = [indexPath retain]; 
     [self.tableView beginUpdates]; 
     [self.tableView endUpdates]; 
} 

上述方法將強制的tableView來調整所選擇的行的高度。但是當我在上面的方法中添加UIButton時,該按鈕不可見。

我對[self.tableView reloadData]不感興趣;

任何幫助非常感謝。

** * **EDITED* ** * ***

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (selIndPath && selIndPath == indexPath) { 
     //UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
//  UIButton* btn = (UIButton*)[[cell contentView] viewWithTag:1234]; 
//  btn.frame = CGRectMake(40, 60, 60, 24); 
//  [cell setNeedsDisplay]; 
     return 90; 
    } 
    return 64; 
} 

- (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] autorelease]; 
     [[cell contentView] setBackgroundColor:[UIColor clearColor]]; 
     [[cell backgroundView] setBackgroundColor:[UIColor clearColor]]; 
    } 

    // THIS LINES OF CODE WORKS ONLY ON reloadData 
    if (selIndPath && selIndPath == indexPath) { 
     UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.frame = CGRectMake(40, 60, 60, 24); 
     btn.tag = 1234; 
     [btn setTitle:@"Hi" forState:UIControlStateNormal]; 
     [cell.contentView addSubview:btn]; 
    } 

    // Configure the cell. 
    cell.textLabel.text = @"Loading.."; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    selIndPath = [indexPath retain]; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    //[self.tableView reloadData]; 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
} 
+0

你能證明你是使用添加的UIButton的代碼? – Louis 2011-12-26 07:54:47

+0

感謝您的回覆,我添加了一些額外的代碼給我的問題。這可能會幫助你理解它。 – prathumca 2011-12-26 08:35:56

回答

2

如果添加它添加按鈕將無法正常工作到cellForRowAtIndexPath的單元格中:。你需要做的是創建一個自定義UITableViewCell(參閱這個問題的回答上如何 - Changing the position of custom UIButton in custom UITableViewCell

在自定義單元格,創建一個方法

-(void) addButtonToCell 
{ 
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn.frame = CGRectMake(40, 60, 60, 24); 
    btn.tag = 1234; 
    [btn setTitle:@"Hi" forState:UIControlStateNormal]; 
    [self.contentView addSubview:btn]; 

} 

當然,你如何添加按鈕和什麼你傳遞給這個方法的論點取決於你,但你得到了主旨。

cellForRowAtIndexPath:,只需添加

if (selIndPath && selIndPath == indexPath) { 
[cell addButtonToCell]; 
} 
+0

感謝您的回覆。當且僅當你調用[tableView reloadData]時,這才起作用。如前所述,我對reloadData不感興趣。由於我的表格有非常大的數據,並且會影響行高變化的動畫效果。 – prathumca 2011-12-26 09:25:38

+0

@prathumca:如何使用此方法 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)動畫 在您的表視圖上。這將只重新加載你想要的行。 – MadhavanRP 2011-12-26 09:57:24

+0

我正在嘗試,但截至目前還沒有運氣。動畫不太平滑,按鈕不能正確顯示/對齊。任何幫助真的很感激。 – prathumca 2011-12-26 10:17:41

相關問題