IOS之 UITableview的简单使用

初学ios写的第一个tabview的demo

1.在ViewController.h文件中 实现接口,定义属性

@interface ViewController : UIViewController<UITableViewDelegate,

UITableViewDataSource>

@property(strong,nonatomic) NSMutableArray *data;


2.在ViewController.m文件中初始化数据- (void)viewDidLoad {
    [super viewDidLoad];
    self.data = @[@"张飞",@"赵云",@"刘备",@"关羽",@"马操",@"孔明",
             @"魏延",@"姜维",@"刘禅"];

}

重写两个重要的方法

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

}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"NameIdentifier";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier
                             forIndexPath:indexPath];
    NSString *name = self.data[indexPath.row];
    cell.textLabel.text = name;
    return cell;

}


3.实现点击弹出dialog的方法

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 声明一个UIAlertView
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:self.data[indexPath.row] delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    // 显示 UIAlertView
    [alert show];
    // 添加延迟时间为 1.0 秒 然后执行 dismiss: 方法
    [self performSelector:@selector(dismiss:) withObject:alert afterDelay:1.0];
}


- (void)dismiss:(UIAlertView *)alert{
    // 此处即相当于点击了 cancel 按钮
    [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];

}


3.看图

IOS之 UITableview的简单使用


4.点击main.storyboard 右键DataSource,delegate拉线关联到Viewcontroller

5.运行

IOS之 UITableview的简单使用