iOS开发UI之UITableView的基本使用

表格控件

一. 设置数据源<UITableViewDataSource>

1. 设置数据源

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置数据源
    self.tableView.dataSource = self;
    
    // 设置代理
    self.tableView.delegate = self;
    
    // 表格头部和尾部显示
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    self.tableView.tableFooterView = [[UISwitch alloc] init];
}

2. 设置有多少组数据

/**
 *  有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carsGroup.count;
    
}

3. 设置每组有多少行数据

/**
 *  每一组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LDCarGroup *carGroup = self.carsGroup[section];
    return carGroup.cars.count;
}

4. 设置每一行的数据

/**
 *  每一行的内容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDCarGroup *carGroup = self.carsGroup[indexPath.section];
    
    // 缓存池的ID标识
    static NSString *ID = @"car";
    
    // 从缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 缓存池中没有可循环利用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }
    
   LDCar *car = carGroup.cars[indexPath.row];
    
    // 1.图像显示
    cell.imageView.image = [UIImage imageNamed:car.icon];
    // 2.标题文字显示
    cell.textLabel.text = car.name;
    // 3.子标题显示
    cell.detailTextLabel.text = @"Good!!!";
    
    // 4.设置cell右边的指示器
    /*
     UITableViewCellAccessoryCheckmark : 显示√
     UITableViewCellAccessoryDetailButton : 显示详情按钮
     UITableViewCellAccessoryDetailDisclosureButton : 显示详情按钮+尖括号
     UITableViewCellAccessoryDisclosureIndicator : 显示 尖括号
     UITableViewCellAccessoryNone : 默认右边指示器什么也不显示
     */
    // 4.1 用系统的指示器
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    // 4.2定义View显示到cell右边
    cell.accessoryView = [[UISwitch alloc] init];
    
    // 5.设置cell的背景色 backgroundView的优先级 > backgroundColor
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"btn_left"];
    cell.backgroundView = imageView;
    
    // 6.设置选中cell时的背景色
    UIImageView *imageView1 = [[UIImageView alloc] init];
    imageView1.image = [UIImage imageNamed:@"img_01"];
    cell.selectedBackgroundView = imageView1;
    
    return cell;
}

5. 组头部描述

/**
 *  头部描述
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    LDCarGroup *carGroup = self.carsGroup[section];
    return carGroup.title;
}

6. 组尾部描述

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return  ;  
}

7. 表格右边导航条显示

/**
 *  返回右边导航条显示字符串
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.carsGroup valueForKeyPath:@"title"];
}

二. 代理监听UITableView<UITableViewDelegate>

1. 监听选中了哪个cell 

/**
 *  监听选中了那个cell
 */
int group = 0;
int row = 0;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDCarGroup *carGroup = self.carsGroup[indexPath.section];
    LDCar *car = carGroup.cars[indexPath.row];
    
    // 设置弹窗
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"车名展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    
    // 设置弹窗类型
    /*
     UIAlertViewStyleDefault : 默认弹窗
     UIAlertViewStyleLoginAndPasswordInput : 显示登录对话框
     UIAlertViewStylePlainTextInput : 显示一个明文输入框
     UIAlertViewStyleSecureTextInput : 显示一个密文输入框
     */
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    // 取得文本框,显示车名
    [alert textFieldAtIndex:0].text = car.name;
    
    // 显示弹窗
    [alert show];
    
    // 获取组号和行号
    group = (int)indexPath.section;
    row = (int)indexPath.row;
}

2. 监听取消选中的cell

/**
 *  监听取消选中cell
 */
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
{
    
}

三. 代理监听UIAlertView弹窗<UIAlertViewDelegate> 

1. 监听点击了UIAlertView弹窗上的哪个按钮

**
 *  监听点击了UIAlertView弹窗上哪个按钮
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 1.如果点击左边的取消按钮直接放回
    if (buttonIndex == 0) return;
    
    // 2.获取文本框最后的文字
    NSString *name = [alertView textFieldAtIndex:0].text;
    
    // 3.修改模型数据
    LDCarGroup *carGroup = self.carsGroup[group];
    LDCar *car = carGroup.cars[row];
    car.name = name;
    
    // 4.刷新数据
    // 4.1 全局刷新数据
    [self.tableView reloadData];
    // 4.2 局部刷新数据
    /*
     UITableViewRowAnimationFade : 闪一下
     UITableViewRowAnimationRight : cell从左边向右滑出刷新
     UITableViewRowAnimationLeft : cell从右边向左滑出刷新
     UITableViewRowAnimationTop : cell从下向上滑出刷新
     UITableViewRowAnimationBottom :
     UITableViewRowAnimationNone : 没有什么效果
     UITableViewRowAnimationMiddle : cell从上向下滑出刷新
     UITableViewRowAnimationAutomatic : 动画闪现
     */
    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:group];
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
原文地址:https://www.cnblogs.com/Xfsrn/p/4842397.html