FirstApp,iphone开发学习总结2,简单的表

首先在TableViewController.h中,将UIViewController改成UITableViewController(委托先不写),并创建一个数组:

@interface TableViewController : UITableViewController{
    NSMutableArray *data;
}

在init方法中,我们初始化此表为分组表:

- (id)init {
    self = [super initWithStyle:UITableViewStyleGrouped];//分组样式
    if (self) {
        [self setTitle:@"表格展示"];
        UIImage *img = [UIImage imageNamed:@""];
        [[self tabBarItem] setImage:img];
    }
    return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

在- (void)viewDidLoad中初始并实例化数组:

data = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];

设置分组数://不是分组表,可以忽略,默认为1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [data count];
}

设置每组的行数:*必须实现//理解section的意思

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 2;
    }else{
        return 3;
    }
}

设置每行的数据:*必须实现//理解标识符(Identifier)的意思

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"FirstAppTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [data objectAtIndex:[indexPath row]];
    return cell;
}

最后在- (void)dealloc释放data:

[data release];

 

 

posted @ 2012-05-03 22:56  Maxfong  阅读(189)  评论(0编辑  收藏  举报