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];

 

 

转载于:https://www.cnblogs.com/maxfong/archive/2012/05/03/2481924.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值