使用uibutton填充UITableCellView

时间:2011-12-22 10:35:45

标签: uitableview uibutton

我的目标是在分组tableview的单元格中插入一个完全填充它的按钮,例如Facebook或Skype在iPad上登录。为此,我使用以下代码:

- (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

2 个答案:

答案 0 :(得分: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...

答案 1 :(得分: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;
}