oc UITableView 列表

这篇具有很好参考价值的文章主要介绍了oc UITableView 列表。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

oc UITableView 列表,oc,ios

 

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"
// 添加协议
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置数据源
    self.tableView.dataSource = self;
    
    
}
// tableView 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
    
}
// tableview第section组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }else if (section ==1){
        return 6;
        
    }else if(section == 2){
        return 6;
    }else{
        return 1;
    }
    
   
    
}
// 显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    if(indexPath.section == 0){ // 第0组
        if(indexPath.row == 0){ // 第0组第0行
            cell.textLabel.text = @"通用";
        }else if(indexPath.row == 1){
            cell.textLabel.text = @"隐私";
            
        }
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"%ld组%zd行-其他数据",indexPath.section,indexPath.row];
    }
    
    return cell;
    
}


@end

oc UITableView 列表,oc,ios

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 oc UITableView 列表,oc,ios

 自定义view

cell.accessoryView = [[UISwitch alloc]init];

 oc UITableView 列表,oc,ios

 oc UITableView 列表,oc,ios

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"
// 添加协议
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置数据源
    self.tableView.dataSource = self;
    // 设置头部和尾部的高度
    self.tableView.sectionFooterHeight = 30;
    self.tableView.sectionHeaderHeight = 30;
    // 这个属性可以设置所有的行高,通过这个属性设置的行高都一样
    self.tableView.rowHeight = 40;
    
    
}
// tableView 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}
// tableview第section组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }else{
        return 3;
    }
    
   
    
}
// 显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    // 右侧有箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 设置cell右边显示的控件
    //cell.accessoryView = [[UISwitch alloc]init];
    if(indexPath.section == 0){ // 第0组
        if(indexPath.row == 0){ // 第0组第0行
            cell.textLabel.text = @"奔驰";
            cell.imageView.image = [UIImage imageNamed:@"m_2_100"];
        }else if(indexPath.row == 1){
            cell.textLabel.text = @"宝马";
            cell.imageView.image = [UIImage imageNamed:@"m_3_100"];
            
        }
    }else if(indexPath.section == 1){
        if(indexPath.row == 0){ // 第0组第0行
            cell.textLabel.text = @"丰田";
            cell.imageView.image = [UIImage imageNamed:@"m_7_100"];
        }else if(indexPath.row ==1){
            cell.textLabel.text = @"马自达";
            cell.imageView.image = [UIImage imageNamed:@"m_18_100"];
        }else if(indexPath.row ==2){
            cell.textLabel.text = @"本田";
            cell.imageView.image = [UIImage imageNamed:@"m_26_100"];
        }
    }
    
    return cell;
    
}

// 每一个组头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"德系品牌";
    }else{
        return @"日系品牌";
    }
    
}
// 每一个组尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if(section == 0){
        return @"德系品牌footer";
    }else{
        return @"日系品牌footer";
    }
}

// 设置section footer的高度



@end

 优化代码。使用模型

oc UITableView 列表,oc,ios

 oc UITableView 列表,oc,ios

oc UITableView 列表,oc,ios

 oc UITableView 列表,oc,ios

 

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"
#import "XMGCarGroup.h"
#import "TableCar.h"
// 添加协议
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,assign) int count;

@property(nonatomic,strong) NSArray *carGroups;



@end


@implementation ViewController

// 懒加载
-(NSArray *)carGroups{
    if(!_carGroups){
        XMGCarGroup *group0 = [[XMGCarGroup alloc]init];
        group0.header = @"德系品牌";
        group0.footer = @"德系品牌XXXX";
        group0.cart = @[
           [TableCar carWithName:@"奔驰" icon:@"m_2_100"],
           [TableCar carWithName:@"宝马" icon:@"m_3_100"],
        ];
        
        XMGCarGroup *group1 = [[XMGCarGroup alloc]init];
        group1.header = @"日系品牌";
        group1.footer = @"日系品牌XXXX";
        group1.cart = @[
           [TableCar carWithName:@"丰田" icon:@"m_1_100"],
           [TableCar carWithName:@"马自达" icon:@"m_18_100"],
           [TableCar carWithName:@"本田" icon:@"m_26_100"],
        ];
        _carGroups = @[group0,group1];
    }
    return _carGroups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置数据源
    self.tableView.dataSource = self;
    // 设置头部和尾部的高度
    self.tableView.sectionFooterHeight = 30;
    self.tableView.sectionHeaderHeight = 30;
    // 这个属性可以设置所有的行高,通过这个属性设置的行高都一样
    self.tableView.rowHeight = 40;
    
    
}
// tableView 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
    
}
// tableview第section组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if(section == 0){
//        return 2;
//    }else{
//        return 3;
//    }
    // 取出section组的组模型
    XMGCarGroup *group = self.carGroups[section];
    return group.cart.count;
   
    
}
// 显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSString *name =nil;
//    NSString *icon = nil;
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    // 右侧有箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 设置cell右边显示的控件
    //cell.accessoryView = [[UISwitch alloc]init];
//    if(indexPath.section == 0){ // 第0组
//        if(indexPath.row == 0){ // 第0组第0行
//            name = @"奔驰";
//            icon = @"m_2_100";
//        }else if(indexPath.row == 1){
//            name = @"宝马";
//            icon = @"m_3_100";
//
//        }
//    }else if(indexPath.section == 1){
//        if(indexPath.row == 0){ // 第0组第0行
//            name = @"丰田";
//            icon = @"m_7_100";
//        }else if(indexPath.row ==1){
//           name = @"马自达";
//            icon = @"m_18_100";
//        }else if(indexPath.row ==2){
//            name = @"本田";
//            icon = @"m_26_100";
//        }
//    }
    // 取出indexPath.section 组模型
    XMGCarGroup *group = self.carGroups[indexPath.section];
    // 取出indexpath.row 对应的车模型
    TableCar *car = group.cart[indexPath.row];
    cell.textLabel.text = car.name;
    cell.imageView.image = [UIImage imageNamed:car.icon];
    
    return cell;
    
}

// 每一个组头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // 取出indexPath.section 组模型
    XMGCarGroup *group = self.carGroups[section];
    return group.header;
    // 取出indexpath.row 对应的车模型
    
}
// 每一个组尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    XMGCarGroup *group = self.carGroups[section];
    return group.footer;
}

// 设置section footer的高度



@end

 文章来源地址https://www.toymoban.com/news/detail-575414.html

到了这里,关于oc UITableView 列表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • iOS数据采集方案-UITableView和UICollectionView点击事件实现

    在移动端开发中,数据采集是非常重要的一环,它能够帮助我们了解用户的行为和使用情况,从而进行产品优化和决策制定。在iOS应用中,全埋点是一种常用的数据采集方式,它可以实现对用户在应用中的各种操作进行监测和记录。本文将介绍如何在iOS应用中通过全埋点实现

    2024年02月06日
    浏览(35)
  • ios oc button 设置

    Button调整内部的子控件的位置            

    2024年02月16日
    浏览(28)
  • 记录一个iOS UITableView 正在刷新的时候修改数据源导致的崩溃

    首先看一下崩溃堆栈信息 由于tableview 调用layoutsubViews 执行到代理方法 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 由于是崩溃在系统方法里面的,我们无法直接看到是因为调用哪个方法导致的崩溃 后来经过多次调试,发现是因为调用了reloadData 出

    2024年01月17日
    浏览(47)
  • 【IOS】oc中property属性值详解

    1. atomic与nonatomic atomic :原子属性确保属性的读取和写入操作在多线程环境中是线程安全的。这意味着,当一个线程正在读取或写入属性时,其他线程不能同时访问该属性。虽然这提供了线程安全性,但它也引入了性能开销,因为每次访问都需要加锁和解锁。 nonatomic :非原子

    2024年02月21日
    浏览(26)
  • OC(iOS)中常见的面试题汇整(大全)

    你如何理解OC这门语言的?谈一下你对OC的理解? ​​​​​​​        OC语言是C语言的一个超集,只是在C语言的基础上加上了面向对象的语言特征,如:继承,封装,多态.        封装:把属性和方法封装成一个类,方便我们使用        多态:不同对象对于同一消息的不同响应,子

    2024年03月18日
    浏览(25)
  • 【iOS开发】理解OC的类,父类,元类的关系

    在OC中,有对象objc,有类Class,有父类SuperClass,其实还有一种元类MetaClass。在接下来的RunLoop,消息转发机制的学习之前需要知道OC类和对象的底层,所以理解类, 父类,元类的关系是基础 在 Objective-C 中,类是对象的模板或蓝图,而对象则是类的实例。每个对象都有一个类作

    2024年02月03日
    浏览(32)
  • (flutter)黑苹果系统 Xcode iOS flutter 跑通真机模拟器 此oc clover 彼oc swift

    前段时间写了关于flutter的一系列基础知识和入门的一些坑,中间把ios端的项目编译部署等工作一带而过,这里我觉得还是有必要专门写一篇文章来讲讲这个,顺便把环境问题也一起说了。 我们都知道开发ios应用需要用到苹果电脑,即使flutter也不例外,flutter编译构建需要Xc

    2024年02月07日
    浏览(38)
  • iOS从UI内存地址到读取成员变量(oc/swift)

    开发调试时,我们发现bug时常首先是从UI显示发现异常,下一步才会去定位UI相关连的数据的。XCode有给我们提供一系列debug工具,但是很多人可能还没有形成一套稳定的调试流程,因此本文尝试解决这个问题,顺便提出一个 暴论 :UI显示异常问题只需要两个步骤就能完成定位

    2023年04月19日
    浏览(62)
  • iOS开发 点击按钮弹出按钮列表菜单

    调用方法如下 效果如下

    2024年02月16日
    浏览(29)
  • Flutter Android & IOS 获取通讯录联系人列表

    1.在 pubspec.yaml 文件中添加 contacts_service 和 permission_handler 插件的依赖: 2.在你的 Dart 代码中,导入 contacts_service 插件: 3.权限请求: Android 需要在 android/app/src/main/AndroidManifest.xml 文件中添加以下内容: IOS 需要在 ios/Runner/Info.plist 文件中添加以下内容: 在ios系统上如果进行

    2024年02月08日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包