`
xuela_net
  • 浏览: 494422 次
文章分类
社区版块
存档分类
最新评论

IOS开发-TableView表视图基础

 
阅读更多


表视图在IOS中的应用非常广泛,常用于展示显示数据列表。


在工具组中包含了Table View 和Table View Cell


每个表示图都是UITableView的一个实例,每个可见行都是UITableViewCelll的一个实例。也就是把TableViewCell放入TableView里面。


表格有两种style

分组和列表

分组:Grouped


列表:Plain




下面做一个默认style的表视图实例:

1.先拉出一个TableView到xib中。


2.连接添加的TableView和ViewController

因为IOS开发是使用MVC模式,所以这里要把视图和控制层关联起来,而他们之间的桥梁就是File's Owner.这样一来控制器类就成了TableView的数据源和委托


3.向ViewController.h中添加

@property (nonatomic,retain) NSArray *list;

并使用

<UITableViewDelegate,UITableViewDataSource>协议

向ViewController.m添加实现

@synthesize list=_list;

4.建立数据

在ViewController.m中添加

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
                      @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",
                      @"日本" , nil];
    self.list = array;
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.list = nil;
}


实现协议的三个方法,这三个方法必须实现不然后果很严重!!!

//绑定数据源
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.list objectAtIndex:row];
    return cell;
}
//是否分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}


运行结果:


这样一个简单的tableView例子就完成了 NICE!

5.为每一行加图片,首先添加图片文件到到项目中

修改方法:- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath;

  UIImage *img= [UIImage imageNamed:@"a.png"];
    cell.imageView.image=img;

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    
    
    UIImage *img= [UIImage imageNamed:@"a.png"];
    cell.imageView.image=img;
    
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.list objectAtIndex:row];
    return cell;
}

效果如下:



6.设置表格的样式,说明文字

initWithStyle:UITableViewCellStyleSubtitle
cell.detailTextLabel.text =@"nice";

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:TableSampleIdentifier];
    }
    
    
    UIImage *img= [UIImage imageNamed:@"a.png"];
    cell.imageView.image=img;
     
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.list objectAtIndex:row];
    
     cell.detailTextLabel.text =@"nice";
   
    return cell;
}

效果如下:



7.设置缩进级别

//缩进级别
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    return row;
}

效果如下:


那么一行会按照它的索引去增加缩进,nice~

8.处理行的选择

可以使用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath这个方法来做处理

//行的选择
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *rowString = [self.list objectAtIndex:[indexPath row]];
    UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alter show];
}

效果如下:



9.设置字体的大小和行高

cell.textLabel.font = [UIFont boldSystemFontOfSize:50];

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:TableSampleIdentifier];
    }
    
    
    UIImage *img= [UIImage imageNamed:@"a.png"];
    cell.imageView.image=img;
     
    NSUInteger row = [indexPath row];
    
    cell.textLabel.text = [self.list objectAtIndex:row];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
     cell.detailTextLabel.text =@"nice";
    return cell;
}

效果如下:

但这样我们又发现行高不对,所以我们需要在控制层类里面再添加一个委托方法来解决这个问题,

//委托解决行高问题
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

效果如下:



那么今天的TableView表视图基础知识就学习到这里。


以下是IOS开发-我的第一个IOS程序IOS开发-TableView表视图基础 源码:

源码下载

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics