iOS OC TableView的使用(1)- 基本使用方法

写在前面

  • 本文使用的IDE为Xcode9.4.1

  • 目的是展示控件TableView的基本使用方法

  • 使用的语言为Objective-C

  • 实现使用TableView控件有两种方法:

    • 1、直接使用Xcode已经提供的TableView Controller。

    • 2、另一种是自己创建ViewController,在其上添加tableView并设置代理。

实现

由于第二种方法更为自由,也体现了第一种的实现原理。所以在此只介绍第二种。

1、打开一个项目,打开.storyboard文件,在Xcode的控件栏找到TableView控件。将它拖动到一个ViewController上。

12738065-08f69a0e26817ff2.png
tableview1.png

2、将TableView的代理设置为当前ViewController:在tableView上按住control键,拉动鼠标到ViewController。选择dataSource和delegate。

12738065-80da4fbc899c9589.png
tableView代理.png

3、找到ViewController代码。在.h文件加入代理协议<UITableViewDataSource,UITableViewDelegate> 以及添加两个成员变量。如下:

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *tableview;
    NSArray *data;//数组的每一个元素对应tableView相应的一格
}

@property(retain,nonatomic) NSArray *data;

@end

4、在当前viewController的.m文件中加入TableView的代理方法。

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *provinces=[[NSArray alloc] initWithObjects:@"湖南",@"湖北",@"山东",@"山西",@"河南",@"河北",@"广东",@"广西",@"黑龙江",@"内蒙古",@"新疆",@"西藏",@"台湾",@"香港",@"澳门", nil];
    self.data = provinces;
    
    tableview.rowHeight = 30;
    //行高
}
 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellTableIndentifier = @"CellTableIdentifier";
    //单元格ID
    //重用单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIndentifier];
    //初始化单元格
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIndentifier];
        //自带有两种基础的tableView样式,UITableViewCellStyleValue1、2. 后面的文章会讲解自定义样式
    }
    
    UIImage *img = [UIImage imageNamed:@"tachi.png"];
    cell.imageView.image = img;
    //添加图片
    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"省份";
    //添加右侧注释
    
    return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    //获取storyboard
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

    //根据storyboard创建控制对象
     Detail * celldetail = [storyboard instantiateViewControllerWithIdentifier:@"celldetail"];

    [celldetail viewDidLoad];

    [self.navigationController pushViewController:celldetail animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

5、 这样tableView基本显示就完成了。

12738065-8308ef39b57cd5f3.png
tableView展示.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值