TableView Summary


Tableview 在ISO中的使用很频繁,我也是刚刚学习ISO开发,就些想就TableView总结一下。有什么错误或补充请大家提出来,Welcome your valuable comments.;)


一、简单的列表。

在开发之前我想大家应该知道TableView有两个重要的委托:UITableViewDataSources, UITableViewDelegate.

这两个一个是提供表的数据模型的,一个是实现选择区域,配置分区头和尾,帮助删除和记录单元格并且执行其它动作(翻译而来)。

UITableViewDataSources 中有两个必须提供的两个方法:

- (NSInteger) tabelView:(UITableView *)tableView numberOfRowInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath;

其中也有许多可选的实现方法:设置行的高度或者显示的缩进,分区,可编辑,可移动等等,这些根据实际的要求自己重写这方法。

这里的分区的意思我简单说下。有三种简单列表 一个是无分区的列表,一个是有分区的列表(例:联系人-根据A-Z分组),还有是一个有分区带索引的列表(例:联系人-根据字母分组且右边列出了一列很小的26个字母)。

这里我只实现一个没有分区的简单列表。

TableView 必须要嵌套在TableViewController里,否则会提示出错,或者不能显示单元格。

创建一个single-view 的applicaton然后有个ViewController它是UIViewController的子类,我们修改在为UITableViewController<UITableViewDataSources,UITableViewDelegate>

然后在object library中选择TableView拖放到ViewController中。

<img src="https://img-blog.csdn.net/20150713105211720?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
这里最重要的是tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath;

代码中创建单元格(Default)并且设置一个图片。


二、创建一个自定义的表格

系统有提供三个类型的单元格,但有时候我们这三种类型的单元格不能满足我们的要求,需要我们自己创建自己想要的单元格。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil)
    {
        CGRect cellframe=CGRectMake(0, 0, 300, 65);
        cell=[[UITableViewCell alloc] initWithFrame:cellframe];
        //add Name label
        CGRect nameLabelRect=CGRectMake(0, 5, 70, 15);
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:nameLabelRect];
        nameLabel.textAlignment=NSTextAlignmentCenter;
        nameLabel.text=@"Name";
        nameLabel.font=[UIFont boldSystemFontOfSize:12];
        [cell.contentView addSubview:nameLabel];
        
        //add Color Label
        CGRect colorLabelRect=CGRectMake(0, 26, 70, 15);
        UILabel *colorLabel=[[UILabel alloc]initWithFrame:colorLabelRect];
        colorLabel.textAlignment=NSTextAlignmentCenter;
        colorLabel.text=@"Color";
        colorLabel.font=[UIFont boldSystemFontOfSize:12];
        [cell.contentView addSubview:colorLabel];
        
        CGRect nameValueRect=CGRectMake(80, 5, 200, 15);
        UILabel *nameValue=[[UILabel alloc] initWithFrame:nameValueRect];
        nameValue.textAlignment=NSTextAlignmentCenter;
        nameValue.tag=kNameValueTag;
        nameValue.text=[[self.computers objectAtIndex:indexPath.row] objectForKey:@"Name"];
        nameValue.font=[UIFont boldSystemFontOfSize:12];
        [cell.contentView addSubview:nameValue];<pre name="code" class="objc">
CGRect colorValueRect=CGRectMake(80, 25, 200, 15); UILabel *colorValue=[[UILabel alloc] initWithFrame:colorValueRect]; colorValue.textAlignment=NSTextAlignmentCenter; colorValue.tag=kNameValueTag; colorValue.text=[[self.computers objectAtIndex:indexPath.row] objectForKey:@"Color"]; colorValue.font=[UIFont boldSystemFontOfSize:12]; [cell.contentView addSubview:colorValue]; }

 

其它与简单列表设置一样,这里的实现方法有点不同,代码直接创建UI对象然后通过方法cell.contentView addSubView将对象添加到视图中。

这个方法比较繁琐还需要计算对象在视图中的位置。

下面介绍另一种方法通过创建一个xib视图文件来自定义自己的单元格。

首先我们创建xib文件,然后拖放一个TableView Cell对象,然后添加相应的控件到TableViewCell中,这里我只绑定两个标签。

然后我们要创建自己的单元格的数据模型。这个我们需要继承UITableViewCell

@interface CustomCell : UITableViewCell
{
    IBOutlet UILabel *_nameLabel;
    IBOutlet UILabel *_colorLabel;
}

@property (nonatomic,retain)UILabel *nameLabel;
@property (nonatomic,retain)UILabel *colorLabel;
@end

下面就是主表中实现代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    NSDictionary *dicRow1=[[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];
    NSDictionary *dictRow2=[[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Silver",@"Color",nil];
     NSDictionary *dictRow3=[[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Air",@"Name",@"White",@"Color",nil];
    NSArray *array=[[NSArray alloc] initWithObjects:dicRow1,dictRow2,dictRow3, nil];
    self.computers=array;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.computers count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString * identifier=@"CustomCellIdentifier";
    CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil)
    {
        //Nib file
        NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }
    NSUInteger row=[indexPath row];
    NSDictionary *rowData=[self.computers objectAtIndex:row];
    cell.nameLabel.text=[rowData objectForKey:@"Name"];
    cell.colorLabel.text=[rowData objectForKey:@"Color"];
    return cell;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值