2010-05-13 60 views
1

我想为我的设置屏幕使用UITableView。因此,我的设置视图看起来像苹果,组表视图,然后用户界面元素,让您更改设置。iPhone:UITableView与自定义单元格设置

所以,它似乎不像单元格从数组中加载,但似乎每个单元都是自定义的。

想法如何做到这一点?

回答

2

对于初学者,请将tableViewStyle属性设置为UITableViewStyleGrouped。 然后更改数据源以提供具有所需组的二维数组,而不是简单的数组。实际上这很简单。

您将需要使用UIControl类型自定义该行 - 我假设您已经这样做了。

编辑:

要控制添加到该行,当您创建单元创建它。

... 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(0.0f, 5.0f, 30.0f, 30.0f)]; 

[button setTitle:@"Click Me!" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
[cell addSubview:button]; 
... 
+0

是的,我已经将它设置为分组。我想我对如何使用一个数组这个问题感到困惑,因为每个单元格都会有不同的布局。 – 2010-05-13 22:40:19

+0

编辑与我认为你在找什么 – psychotik 2010-05-13 22:59:52

0

你可以不用数组。只需使用以下方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

然后可以使用switch-method来解析所有行和段。这两个方法在顶部的方法头上,你可以将它缩短一点。

NSUInteger row = [indexPath row]; 
NSUInteger section = [indexPath section]; 

不要忘记,手动tableviews容易出错。

欢呼西蒙