UITableView(表视图)使用详解

https://blog.csdn.net/q562679764/article/details/48553647
tableview分组样式每组section之间距离的调整
https://www.jianshu.com/p/6206d2c8efe0
iOS 固定UITableView的cell.imageView.image图片大小
https://www.jianshu.com/p/1d9b6e95f0f3

//
//  ViewController.m
//  UITableView-表视图控件
//
//  Created by Liu,Wenbo(TBRD) on 2020/5/29.
//  Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//

#import "ViewController.h"
//符合UITableViewDelegate,UITableViewDataSource协议
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_dataArray;
    UITableView *tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化数据源数组
    _dataArray = @[@"1",@"2",@"3"];
    
    //创建一个tableView
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStyleGrouped];
    /**样式
        typedef NS_ENUM(NSInteger, UITableViewStyle) {
            UITableViewStylePlain,          // regular table view  默认平铺的风格
            UITableViewStyleGrouped,        // sections are grouped together 分组的风格
            UITableViewStyleInsetGrouped  API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos)  // grouped sections are inset with rounded corners
        };
     */
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    
    [btn setTitle:@"编辑" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
    
    //设置分割线内容部分的位置
    //viewTable.separatorInset = UIEdgeInsetsMake(0, 40, 0, 0);
    
    //设置代理和数据源
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
}

//action方法 通过按钮的方法 改变tableview的编辑模式
-(void)action:(UIButton *)btn{
    if(tableView.isEditing){
        tableView.editing = NO;
        [btn setTitle:@"编辑" forState:UIControlStateNormal];
    }
    else{
        tableView.editing = YES;
        [btn setTitle:@"完成" forState:UIControlStateNormal];
    }
}


#pragma mark - 实现代理方法
//设置分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}

//设置每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
            
        case 1:
            return 2;
            break;
        
        default:
            return 1;
    }
}

// 设置每个行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){//第一个分区
        return 60;
    }
    else if(indexPath.section == 1){//第二个分区
        if(indexPath.row == 0){ //第二个分区的第一行
            return 40;
        }
        else{
            return 30;
        }
    }
    return 44;
}

//对tableViewCell中的 行 进行设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    UITableViewCell *cell = [[UITableViewCell alloc] init];
//    cell.textLabel.text = _dataArray[indexPath.section * 1 + indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

//设置tableView的头视图
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//    view.backgroundColor = [UIColor blueColor];
//    return view;
//}

//设置tableView的尾视图
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
//    view.backgroundColor = [UIColor redColor];
//    return view;
//}


//设置头视图的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 30;
//}
//设置尾视图的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 30;
//}

//设置tableView被编辑时的状态风格,如果不设置,默认都是删除风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        //在第一分支 是插入风格
        return UITableViewCellEditingStyleInsert;
    }
    else{
        // 在第二分支 是删除风格
        return UITableViewCellEditingStyleDelete;
    }
    
}

//tableView接受编辑时调用的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    /**commitEditingStyle 编辑风格
     typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
         UITableViewCellEditingStyleNone,没有编辑操作
         UITableViewCellEditingStyleDelete,删除 (默认)
         UITableViewCellEditingStyleInsert  插入操作
     };
     */
    NSLog(@"在这里处理数据,section = %li,row = %li",indexPath.section,indexPath.row);
}

//设置每个分区头部的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%li",section + 1 ];
}

//tableView索引栏相关代理方法
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}

//设置索引栏标题对应的分区
-(NSInteger )tableView:(UITableView *)tableView sectionForSectionIndexTitle:(nonnull NSString *)title atIndex:(NSInteger)index{
    NSLog(@"选中%ld分区",index + 1);
    //NSLog(@"%li",tableView.numberOfSections);//有多少分区
    return tableView.numberOfSections - 1;
}

@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在iOS中,一个视图只能有一个UITableView。但是可以通过创建多个UITableView来实现一个视图中显示多个格的效果。以下是一个示例代码: 首先,你需要在视图控制器中添加多个UITableView的实例变量: ```swift class YourViewController: UIViewController { var tableView1: UITableView! var tableView2: UITableView! // ... } ``` 然后,在视图加载完成后,你可以创建和配置这些UITableView的实例: ```swift override func viewDidLoad() { super.viewDidLoad() // 创建第一个UITableView tableView1 = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2)) tableView1.dataSource = self tableView1.delegate = self view.addSubview(tableView1) // 创建第二个UITableView tableView2 = UITableView(frame: CGRect(x: 0, y: view.frame.height/2, width: view.frame.width, height: view.frame.height/2)) tableView2.dataSource = self tableView2.delegate = self view.addSubview(tableView2) // ... } ``` 接下来,你需要实现UITableViewDataSource和UITableViewDelegate协议的相关方法来提供格的数据和处理交互事件。例如: ```swift extension YourViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == tableView1 { // 返回第一个UITableView的行数 return 10 } else if tableView == tableView2 { // 返回第二个UITableView的行数 return 5 } return 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) if tableView == tableView1 { // 配置第一个UITableView的单元格 cell.textLabel?.text = "Table View 1 - Row \(indexPath.row)" } else if tableView == tableView2 { // 配置第二个UITableView的单元格 cell.textLabel?.text = "Table View 2 - Row \(indexPath.row)" } return cell } } extension YourViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView == tableView1 { // 处理第一个UITableView的行选中事件 print("Table View 1 - Row \(indexPath.row) selected") } else if tableView == tableView2 { // 处理第二个UITableView的行选中事件 print("Table View 2 - Row \(indexPath.row) selected") } } } ``` 这样,你就可以在同一个视图使用多个UITableView了。记得在视图控制器中遵循UITableViewDataSource和UITableViewDelegate协议,并在视图加载完成后设置数据源和代理。 希望这能帮到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值