如何更改uitableviewcell的宽度
问题描述:
我将uitableview单元格作为子视图添加到UIView中,这也是ViewController视图的子视图。但由于某种原因,当我将其初始化为更小的尺寸时,UITableViewCell仍然在屏幕上延伸320像素。如何更改uitableviewcell的宽度
即使我将视角看成小视角,仍然保持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];
//..
答
不幸的是,没有办法直接更改UITableViewCell
的宽度:他们将占用的宽度UITableView
,默认情况下,表格视图将占用整个屏幕宽度。使单元格宽度变小的唯一方法是通过更改其框架宽度来使表格视图变小。
dag唠叨吧!这太痛苦了......你认为苹果会皱着眉头把细胞移到左边,所以它稍微偏离视线,然后把细胞标题移到右边。 – HurkNburkS
不能肯定地说。苹果公司严格遵守接口指南。从听起来像,你使用的是分组表格样式,因为这是唯一的边框可见的单元格类型(普通表格类型只显示顶部和底部边框)。我的直觉是,只要所有的单元格边界都可见,他们就不会太在乎你用什么代码来缩小它们。 –
好吧那么这个问题怎么样,你可以有UITableView右侧的部分滑块的部分标题 – HurkNburkS