UITableView小结

接触了两天的UITableView,由于目前我们编程采用的是MRC管理内存模式,所以最近各种内存问题随之出现了,各种滑动TableView随后进行一系列的操作(譬如删除Cell,增加Cell等等),然后程序崩溃。
常见的崩溃现象有下面几种:
1.刚要滑动UITableView程序立刻崩溃,这种情况通常是获许数据时,定义了相关的属性(数组,字典),通常定义完属性之后,我们立刻会重写dealloc方法。此时,如果我们在下面采用便利构造器进行数组或者是字典的初始化的时候,采用带下划线变量名的方式,程序必崩无疑,此时我们需要采用点语法调用setter使其引用计数加1,进行数据的持有。
2.UITableView中的dataSource中必须要实现的两种方法必须要实现。
3.还有一些常见的崩溃问题在对Cell进行操作的基础上,程序中的代码理解不到位,导致的程序崩溃。最常见的就是删除Cell时,对数组和字典的操作,问题的关键是要将数据源与Cell对应起来。

上面是我在学习UITableView遇到的一些问题,既然是对UITableView做一个简单地小结,还是将它的简单地用法说明一下吧!

 UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];

上面这段代码是一个UITableView必须要写的步骤,还有一些属性,你可以根据你的需求进行添加。例如rowHeight是设置每一行的高度,当然你也可以用代理中的方法对每一行设置不相同的高度;cell.imageView.image是在添加一个图片;cell.accessoryType设置辅助视图;cell.detailTextLabel.text相当于副标题的作用等等。

dataSource中必须要实现的两个方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
特定分区的行数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
给每一行提供数据,一共有多少行就执行多少次 这段代码中indexPath的两个属性section和row自己会进行自增。

这里说明一下UItableViewCell采用的重用机制,来减少Cell的创建,采用MutableSet来管理Cell,具体思路如下:
1.先判断重用池里面是否有cell,如果有cell直接从重用池里面取出Cell
2.如果没有Cell创建Cell对象
3.配置Cell上面现实的数据

还有一个可以选择实现的方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  // 返回tableView一共多少个分区。
    return 3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 点击Cell后跳转界面或者是其他操作必须要实现的方法
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ) {
        return 50;
    }
    else{
        return 100;
    }
    // 设置每一个分区的行高
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *aa = @[@"A",@"B",@"C",@"23455",@"1234"];
    return aa;
// 右侧快速索引(可以快速定位到相应的分区)
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 给想要的分区起个名字。
}

下面说明的一下方法是对UITableView进行编辑要实现的方法:
1.删除Cell或者添加Cell要实现的几个步骤:

#warning 1-1让tableView处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
   // self.navigationItem.rightBarButtonItem = self.editButtonItem;
   // 点击EditButtonItem 的时候,会调用的方法。
   // EditButtonItem 按钮再点击的过程中慧自动改变Editing的状态,Edit对应yes, done 对应no 这之间的切换在系统自己内部已经实现,我们只需要调用[super setEditing:editing animated:animated];就行
}
#warning 1-2设置哪些行可以编辑,默认情况下所有行都可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#warning 1-3 设置可编辑行的编辑状态。默认的编辑状态时删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return UITableViewCellEditingStyleInsert |  UITableViewCellEditingStyleDelete;

        return UITableViewCellEditingStyleDelete;
}

#warning 1-4 完成编辑后需要完成什么事情。例如:点击删除按钮,删除相应的行,点击添加按钮,添加一行数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete){
        // 在进行删除的时候,应该先删除数据源,然后再删除对应的行。原因是做操作的时候,会自动调用numberOfRowsInSection方法,如果先删除行,其数据源和行数还是对应不起来,多以应该先对数据源进行操作,在对行进行操作。
#pragma 方法一

        [_array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
#pragma 方法二
//        [tableView beginUpdates];
//        // 无关循序的执行
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//            [_array removeObjectAtIndex:indexPath.row];
//        [tableView endUpdates];

#pragma 方法三
//        [_array removeObjectAtIndex:indexPath.row];
//        [tableView reloadData];

#pragma 方法四
//        [_array removeObjectAtIndex:indexPath.row];
//        NSIndexSet *accc = [NSIndexSet indexSetWithIndex:0];
//        [tableView reloadSections:accc withRowAnimation: UITableViewRowAnimationFade];
    }
 }

2.对UITableView中的Cell进行移动

#warning 2-1让tableView处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
   // 实现tableView的编辑状态
    // 点击EditButtonItem 的时候,会调用的方法。
    // EditButtonItem 按钮再点击的过程中慧自动改变Editing的状态,Edit对应yes, done 对应no 这之间的切换在系统自己内部已经实现,我们只需要调用[super setEditing:editing animated:animated];就行
}
#warning 2-2设置那些行可以移动。默认是不可以移动的
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#warning 2-3 从某一行移动到另外一行时需要做什么事情

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"%@,%@",sourceIndexPath,destinationIndexPath);
    // 调用retain防止野指
    NSString *str = [_array[sourceIndexPath.row]retain];
    [self.array removeObject:str];
    [self.array insertObject:str atIndex:destinationIndexPath.row];
    [str release];
}
#warning 2-4 限定划分区域移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    /*
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else{
    return sourceIndexPath;
    } // 这段代码表示不能跨区域移动
    */

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else{
        return proposedDestinationIndexPath;
    } // 这段代码表示能跨区域移动

}

通常UITableView与容器控制器还有轮播图配合使用,所以UITableView必须要重点掌握!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值