初学UITableView(iOS)

本文详细介绍如何使用Xcode创建UITableView,包括数据源文件的生成、单元格的定制、编辑操作及索引设置等,深入解析UITableView的高级功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备数据:

通过Xcode生成数据源文件 NumberGroup.h NumberGroup.m;

.h文件为头文件 写声明

.m文件为源文件  写实现的方法

NumberGroup.h:

#import <Foundation/Foundation.h>

@interface NumberGroup : NSObject

@property (nonatomic,copy) NSString *groupHeader;
@property (nonatomic,copy) NSString *groupFooter;
@property (nonatomic,copy) NSString *groupIndex;
@property (nonatomic,strong) NSMutableArray *groupNumbers;

+(instancetype) numberGroup;

@end

@property一般可存放三种类型的属性,一般我们可以选择其中的一种或者即为@property(A,B,C)

a.第一种表示的是可读性

readonly:表示该变量只是可读,但是不能修改。

readwrite:表示变量的值既可读也可以修改。

b.第二种表示原子性

natomic:默认属性,在多线程情况下会生成一些互斥加锁代码,避免变量的读写不同步的问题。

nonatomic:非原子性访问,不加同步,多线程并发访问会提高性能。

c.第三种表示内存管理

assign:setter 方法直接赋值,引用计数器不会增加,为了解决原类型与环循引用问题,一般用于简单的数据类型int,float等就用它。

retain:使用reatin的话,赋值的时候引用计数器会+1,防止被内存释放掉。拷贝类型属于指针拷贝。

copy:使用copy的话,赋值的时候引用计数器会+1,防止被内存释放掉。拷贝类型属于内容拷贝。

strong:强引用,其存亡直接决定了所指对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示列表中,则此对象会被从内存中释放。简单讲strong等同于retain。

weak:弱引用,不决定对象的存亡。即使一个对象被持有无数个若引用,只要没有强引用指向他,那麽其还是会被清除。简单讲weak比assign多一个功能,就是当对象消失之后会自动把指针变为nil。

instancetype只能作为函数返回值,不能用来定义定义变量。

用instancetype作为函数返回值返回的是函数所在类型的类型。

NSMutableArray:可变数组  NSArray为不可变数组

NumberGroup.m:

#import "NumberGroup.h"

@implementation NumberGroup

+(instancetype) numberGroup {
    NumberGroup *numberGroup = [[NumberGroup alloc]init];
    
    //设置数据
    numberGroup.groupHeader = [NSString stringWithFormat:@"Header:%d",arc4random_uniform(1000)];
    numberGroup.groupFooter = [NSString stringWithFormat:@"Footer:%d",arc4random_uniform(1000)];
    numberGroup.groupIndex = [NSString stringWithFormat:@"%d",arc4random_uniform(10)];
    
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i = 0; i<10; i++) {
        NSString *number = [NSString stringWithFormat:@"cell:%d",arc4random_uniform(100000)];
        [tempArray addObject:number];
    }
    numberGroup.groupNumbers = tempArray;
    return numberGroup;
}

@end

创建TableViewController

创建TableViewController.h 和TableViewController.m文件

在Main.storyboard中拖出一个TableViewController  点击title ->右侧第三个图标 Custom Class  Class填写TableViewController

如果是第一个页面 第四个图标 View Controller ->Title is Initial View Controller 勾选

TableViewController.h

继承UITableViewController

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end

TableViewController.m

准备数据

定义一个属性为NSMutableArray的数组

@property(nonatomic,strong) NSMutableArray *dataList;

给数组赋值

-(NSMutableArray *)dataList {
    if(_dataList == nil){
        _dataList = [NSMutableArray array];
        
        for (int i = 0; i < 4; i++) {
          /*  NSString *numberString = [NSString stringWithFormat:@"%d",
                                      arc4random_uniform(1000)];
            [_dataList addObject:numberString];
           */
            NumberGroup *numberGroup = [NumberGroup numberGroup];
            [_dataList addObject:numberGroup];
        }
    }
    return _dataList;
}

可以给TableView设置背景颜色等属性 如果不使用Main.storyboard 界面的生成也在这个方法里写

-(void)viewDidLoad{
    [super viewDidLoad];
    self.tableView.sectionIndexColor = [UIColor redColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
}

数据源方法实现

dataList.count:多少行数据

dataList[section]:多少段

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return self.dataList.count;
    
    //取出对应的section
    NumberGroup *tempGroup = self.dataList[section];
    return tempGroup.groupNumbers.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *)indexPath {
    
    //定义一个静态的CELL标识符
    static NSString *CellID = @"cell";
    //不规范
    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    //优先从缓存池中查找ID为“cell”的单元格
    //规范
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    //如果缓存池中没有 则创建
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:CellID];
    }
    
    //获取数据源
   // NSString *numberString = self.dataList[indexPath.row];
    //设置cell的内容
   // cell.textLabel.text = numberString;
    //cell.detailTextLabel.text = numberString;
    //cell.imageView.image = [UIImage imageNamed:@"1"];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //分段方法 section:段 row:行
    NumberGroup *tempGroup = self.dataList[indexPath.section];
    NSString *numberString = tempGroup.groupNumbers[indexPath.row];
    //设置cell的内容
    cell.textLabel.text = numberString;
    cell.detailTextLabel.text = numberString;
    cell.imageView.image = [UIImage imageNamed:@"1"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //自带4种模式
    /*
     UITableViewCellAccessoryNone,                                                      // don't show any accessory view
     UITableViewCellAccessoryDisclosureIndicator,                                       // regular chevron. doesn't track
     UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED,                 // info button w/ chevron. tracks
     UITableViewCellAccessoryCheckmark,                                                 // checkmark. doesn't track
     UITableViewCellAccessoryDetailButton
     */
    // Configure the cell...
    
    NSLog(@"cell:%@",cell);
    return cell;
}

 

单元格的点击方法 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *infoString = [NSString stringWithFormat:@"第%d段,第%d行",indexPath.section,indexPath.row];
    UIAlertView *alartView = [[UIAlertView alloc]initWithTitle:@"提示" message:infoString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alartView show];
}

单元格的编辑 如左划删除

//nullale表示对象可以是空或nil 而_nonnull表示对象不应该为空
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"刷新" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [tableView reloadData];
    }];
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.dataList removeObjectAtIndex:indexPath.row];
        //刷新表格
        [tableView reloadData];
    }];
    
    NSArray *actionArray = @[action1,action2];
    return actionArray;
}

设置header footer 和 索引

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NumberGroup *numberGroup = self.dataList[section];
    return numberGroup.groupHeader;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NumberGroup *numberGroup = self.dataList[section];
    return numberGroup.groupFooter;
}

#pragma mark - 设置索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *indexArray = [NSMutableArray array];
    for (NumberGroup *numberGroup in self.dataList) {
        [indexArray addObject:numberGroup.groupIndex];
    }
    return indexArray;
}

订制section的Header/Footer

-(nullable UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    imageView.image = [UIImage imageNamed:@"1"];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 100, 20)];
    label.text = @"猜你喜欢";
    label.font = [UIFont systemFontOfSize:14.0];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 5, 60, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickOpenButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *headerView = [[UIView alloc]init];
    [headerView addSubview:imageView];
    [headerView addSubview:label];
    [headerView addSubview:button];
    
    return headerView;
}

-(void) clickOpenButton{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"点击了header" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertView show];
}

参考:网易云课堂 详解UITableView

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值