【UIKit】UITableView.06

UITableView.06:

【1】拖入ToolBar,TableView

【2】连线,设置代理模式,数据源等(ToolBar中的垃圾桶也需要连接方法removeRow)

  

【3】代码

1.声明

mydata    :所有的数据,人工初始化的数据30行。
selectedData:选中的数据,视频中点击后选中的数据
selectedRows:只是为了删除时候能得到所选择的数据行而创建的数组
@interface ViewController ()
// 所有的总数据
@property (strong,nonatomic) NSMutableArray *mydata;
// 选中的数据
@property (strong,nonatomic) NSMutableArray *selectedData;// 拿到被选中的数据内容
/********* 这个是用来显示删除动画而选中的数据行 ********/
@property (strong,nonatomic)NSMutableArray *selectedRows;
@end

2.初始化数据

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 初始化数据
    self.mydata=[NSMutableArray array];
    self.selectedData=[NSMutableArray array];
    self.selectedRows=[NSMutableArray array];
    //创建30个对象存放到数组mydata中去   for(int i=0;i<30;i++)
    {
        NSString *text=[NSString stringWithFormat:@"哈哈哈--%d",i];
        [self.mydata addObject:text];
    }
}

3.设置返回行数

#pragma mark- 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mydata.count;
}

4. 判断选中的需要不需要打钩

#pragma mark 每当有一个cell进入视野范围内就会调用,返回当前这行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.用static修饰的局部变量,只会初始化一次< # # >
    static NSString *ID = @"Cell";

    // 1.拿到一个标识先去缓存池中查找对应的Cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.如果缓存池中没有,才需要传入一个标识创建新的Cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    } 

  // 取出当前这行数据【判断是否打钩】
    NSString *curentText=self.mydata[indexPath.row];

    // 3.覆盖数据
    cell.textLabel.text = self.mydata[indexPath.row];

   // 4.覆盖状态【判断是否打钩】
    if([self.selectedData containsObject:curentText]) // 判断选中的这行数据内容和我数组里面的内容是否相等,如果相等
    {
    //需要打钩
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    return cell;
}

5.选中方法,调用,需要使用代理监听

     
/******************************************** 刷新,更新界面 *************************/
 [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle]; 
// 刷新当前行
 [tableView reloadData];  
// 全局刷新
 
#pragma mark- 代理方法
#pragma mark 该方法选中某一行就会调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.通过代码取消选中当前这行(去掉蓝色背景)【点击一下背景变蓝色,闪烁一下】
    [tableView deselectRowAtIndexPath:indexPath animated:YES];   // animated 是否需要动画

    // 2.取出这行对应的数据
    NSString *currentText = self.mydata[indexPath.row];

    // 3【新】.应用MVC修改数据控制界面内容
       if([self.selectedData containsObject:currentText])
       {
           // 删除数据
           [self.selectedData removeObject:currentText];
           /****显示删除动画的数组中移除这个行****/
           [self.selectedRows removeObject:indexPath];

       }else
       {
           // 添加数据
           [self.selectedData addObject:currentText];
           /****显示删除动画的数组中添加这个行****/
           [self.selectedRows addObject:indexPath];
       }

        /************************ 刷新,更新界面 *************************/
       [tableView reloadRowsAtIndexPaths:@[indexPath] // 刷新这一行
                        withRowAnimation:UITableViewRowAnimationMiddle]; // 刷新当前行
      // [tableView reloadData];   // 全局刷新
}
  //【与上面【3】新,同样的操作】
 /*************下面注释掉的是直接进行界面的更改,违反了MVC模式,所以不建议使用**************/
    /*
    // 3.让选中的行打钩,对应的cell打钩
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];   // 这个方法就是通过indexPath得到所

        // 判断selectedData数组中是否包含currentText,如果已经包含(说明已经打钩)
    if([self.selectedData containsObject:currentText])
    {
    // 取消打钩
        cell.accessoryType=UITableViewCellAccessoryNone;
    // 删除数据
        [self.selectedData removeObject:currentText];

    }
    else
    {
    // 没有包含这行数据,(说明没有打钩)
        //  打钩
          cell.accessoryType=UITableViewCellAccessoryCheckmark;
        // 添加数据
          [self.selectedData addObject:currentText];

    }*/

6.设置点击垃圾桶按钮操作

注意:

作用 代码 注意事项
刷新选中单行
[self.tableView reloadRowsAtIndexPaths:self.selectedRows
 withRowAnimation:UITableViewRowAnimationMiddle];
数据个数要保持不变,否则也要报错
删除选中单行
[self.tableView deleteRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationLeft];
比如说,移除了3行,那么对应的数组中也需要移除3行。
#pragma mark - 按垃圾桶删除对应的行
-(IBAction)removeRow
{
    // 删除数据分两步
        // 1.更改数据
    [self.mydata removeObjectsInArray:self.selectedData];

        // 2.刷新UI界面
    // [self.tableView reloadData];

    /*******这个方法的要求:再数据count不变。刷新固定的那几行********/
    /*【[self.tableView reloadRowsAtIndexPaths:self.selectedRows
                          withRowAnimation:UITableViewRowAnimationMiddle];
    】*/

    /*******这个方法的要求:这个使用需要也是数组中删除一定的数据(这个方法里挪掉多少个,数据也要挪掉多少个)********/
    [self.tableView deleteRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationLeft];
        // 3.清除选中的所有数据
    [self.selectedData removeAllObjects];
    [self.selectedRows removeAllObjects];
}

其他:

  遵循协议后【取消选中将监听的方法】

#pragma mark 取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消选中了%d",indexPath.row);
}

【UIKit】UITableView.06,布布扣,bubuko.com

时间: 2024-08-08 05:35:31

【UIKit】UITableView.06的相关文章

【UIKit】UITableView.04

UITableView.04: [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITableViewDataSource> [4]加入图标文件 [5]代码 1.创建一个Product类,用来作为对象内容表示产品信息 2.在Product.h中添加声明代码 @interface Product : NSObject /*********设置产品内容信息*********/ /* 图片*/ @

【UIKit】UITableView.05 性能优化

UITableView.05 性能优化: [为何要性能优化]:TableView拖屏过程中,不断有对象"消失"在屏幕上,但是这样的对象还是存在的,当拖拉过多后,导致内存严重泄漏. 解决方法:仅提供有限的对象,拖拉过程中,只是将内存地址存放的内容进行改变. [旧代码] -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITable

【UIKit】UITableView.02

UITableView.02: [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITableViewDataSource> [4]将数据plist文件拖入 [5]代码 1.viewDidLoad只加载一次,所以用来加载plist文件中的数据. 新建一个bundel用来指定文件路径 pathForResource:文件名 ofType:文件格式 - (void)viewDidLoad { [

【UIKit】UITableView.09 自定义cell

UITableView.09 自定义cell : 注意:在创建一个故事版的时候,需要将控制器的class修改成对应的class否则效果实现不了[如图] 1.这段代码就是用来设置cell所对应的xib,类似于绑定  // 1.想要使用文件包里面的资源就要使用[NSBundle mainBundle] // 2.loadNibNamed的意思是加载一个xib文件,名字为BookCell cell=[[[NSBundle mainBundle]loadNibNamed:@"BookCell"

【UIKit】UITableView.07 编辑模式

[1]拖动好界面 [2]设置协议,数据源 [3]代码 1.声明可变数组,用来存放所有数据对象 @interface ViewController () @property(nonatomic,strong)NSMutableArray *mydata; @end 2.初始化数据[创建30个对象数据] - (void)viewDidLoad { [super viewDidLoad]; self.mydata=[NSMutableArray array]; for(int i =0;i<30;i+

【UIKit】UITableView.01

UITableView.01: section:组别 row:行号 [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITableViewDataSource> 代码 1.加入显示数据内容 - (void)viewDidLoad { [super viewDidLoad]; [email protected][@"广州" ,@"梅州",@"深

【UIKit】UITableView.08 常用属性

UITableView.08 常用属性:  以上图片转自http://blog.csdn.net/totogo2010/article/details/7642908 以上图片转自http://blog.csdn.net/totogo2010/article/details/7642908 1.设置Section的数量 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return TitleData; }

【UIKit】UITableView.03

UITableView.03: [1]拖入一个UITableView [2]将TableView的dataSource与控制器连接 [3]首先得遵循UITableView的数据源协议<UITableViewDataSource> [4]加入图标文件 [5]代码 1.设置一共多少组,系统默认是1组,所以不写的话就默认1组 #pragma mark 返回多少组 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //

【UIKit】控制器之间的切换2 【Push】

[控制器切换Push][?Code] 1.创建3个xib和控制器 2.在AppDelegate中设置首先启动的页面 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds] ]; self.