如何改变uitableviewcell的宽度

时间:2012-08-23 04:16:42

标签: iphone ios uitableview subview

我将uitableview单元格作为子视图添加到UIView中,这也是ViewController视图的子视图。但出于某种原因,当我将UITableViewCell初始化为更小的东西时,UITableViewCell仍在屏幕上延伸320像素。

即使我将视图调整为小视图,它仍然保持320像素宽。我想知道它是否是一个层次结构问题?我觉得我的代码是正确的,但也许我只是错过了一些小事......

//..
 // This UIView holds both custom UITableViewCell and UIButton and is placed at the top of the current UIView
    cellContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 44.0)];
    cellContainer.backgroundColor = [UIColor greenColor];

    // Select all UITableViewCell
    selectAllCell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 7.0)];
    selectAllCell.textLabel.text = @"Select all"; // Set text
//    selectAllCell.accessoryType = UITableViewCellAccessoryCheckmark;
    selectAllCell.backgroundColor = [UIColor redColor]; // set bg color

    // Create non visible UIButton that sits over customTableviwecell wich allows you to set a selctor on the button press
    UIButton *selectAllButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 50.0)];
    [selectAllButton addTarget:self action:@selector(selectAll:) forControlEvents:UIControlEventTouchUpInside];

    // Add custom Cell and UIButton to cellContainer view
    [cellContainer addSubview:selectAllCell];
    [cellContainer insertSubview:selectAllButton aboveSubview:selectAllCell];
    // Ass cellContainer to self.view
    [self.view insertSubview:cellContainer atIndex:1];
//..

1 个答案:

答案 0 :(得分:2)

不幸的是,没有办法直接更改UITableViewCell的宽度:它们会占用UITableView的宽度,默认情况下,表格视图会占用整个屏幕宽度。使单元格宽度变小的唯一方法是通过更改其框架宽度使表格视图本身更小。