2011-12-22 160 views
1

我的目標是在分組tableview中插入一個完全填充它的按鈕,如Facebook或iPad上的skype登錄按鈕。要做到這一點,我用這個代碼:使用UIButton填充UITableCellView

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Login"; 
    UITableViewCell* cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil){ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if(indexPath.section == 1 && indexPath.row == 0){ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:cell.frame]; 
    [button setTitle:@"Do Stuff" forState:UIControlStateNormal]; 
    [cell addSubview:button]; 
    } 
    return cell; 
} 

但結果是這樣的:

UIButton on cell of tableview

是不是我想要的按鈕比細胞更廣泛,它的位置不正確。 我解決了做各種測試,以找到按鈕框架的正確值,但我認爲這不是最好和最優雅的解決方案。有沒有人有比我更好的解決方案?

而是與此代碼:

if(indexPath.section == 1 && indexPath.row == 0){ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:cell.bounds];//note here bounds 
    [button setTitle:@"Do Stuff" forState:UIControlStateNormal]; 

    [cell.contentView addSubview:button]; 
} 
    return cell; 

結果是:

enter image description here

回答

1
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Login"; 
    UITableViewCell* cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil){ 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    if(indexPath.section == 1 && indexPath.row == 0){ 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//note here 
     [button setFrame:cell.bounds];//note here bounds 
     [button setTitle:@"Do Stuff" forState:UIControlStateNormal]; 

     cell.clipsToBounds=YES;//note here 



     [cell.contentView addSubview:button];//note here contentview 
    } 


    } 

    return cell; 
} 
+0

按鈕左上角的頂點與單元格的頂點一致,但仍大於單元格。在我的問題中,我改變了一個更有意義的形象。 – LuckyStarr 2011-12-22 10:54:56

+0

請以圖片形式發佈你需要什麼樣的最終結果 – 2011-12-22 10:57:19

+0

更新只需粘貼此代碼 – 2011-12-22 11:22:16

2

這是正確的答案。關鍵是設置autoresizingMask,如下面的代碼示例所示。

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString* CellIdentifier = nil; 
    if (indexPath.section == BTNSCTN && indexPath.row == BTNROW) { 
     CellIdentifier = @"ButtonCell"; 
     UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (!cell) { 
      cell = [[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier]; // autorelease if not using ARC 

      UIButton* buttonUp = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      [buttonUp setTitle:@"Shake it up!" forState:UIControlStateNormal]; 
      [buttonUp addTarget:self action:@selector(shakePhone) 
       forControlEvents:UIControlEventTouchUpInside]; 
      buttonUp.frame = cell.contentView.bounds; // or cell.bounds 
      buttonUp.autoresizingMask = 
      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
      [cell.contentView addSubview:buttonUp]; 
     } 
     return cell; 
    } 
    else 
     // handle the other sections and cells...