@interface RootViewController : UITableViewController {}
作为本身来说是一个比较单纯的table。
另外一种就是比较广泛的只把入口事件搞来就行,窗口里除了table还可以有很多别的东西,比如
@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{}
不管那种方式,后面的实现都是一样的。
到.m文件里有3个东西是必须的。
1。确定表格数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 表格数量;
}
如果是要显示从其他类传进来的数组,就
return [数组名 count];
绝对不会出错。不过要注意的是最好把传进来的数组retain一下,反正这数组肯定是全局的,那最后统一释放就行,先retain免得它自己想不通自动释放了。
2。画表格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell"; //这样搞一下免得每次都去做,节约一点是一点
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //简单表格定死的做法,复杂的之后再说,下面也是。套路。
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[[cell textLabel] setText:@“想在表格里显示的文字”];
return cell;
}
3。事件
如果仅仅是个list,不需要点击以后发生什么时候,比如显示详细内容的话,这个方法可以不要。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"你摸了第%d行。",indexPath.row);
}
还有一个会容易用到的方法是定制表格高度
我想把表格变成100像素高,这样搞一下
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0;
}
到此为止,复杂表格另外开贴。












发表评论 评论 (0 个评论)