zoukankan      html  css  js  c++  java
  • iOS 点击cell下拉

    iOS  点击cell下拉

    代码如下:

    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    @property (nonatomic, copy) NSMutableArray *muArr;
    @property (nonatomic, copy) NSMutableArray *boolArray;
    @property (nonatomic, strong) UITableView *tableView;
    
    @end
    
    @implementation ViewController
    #pragma mark - LifeCycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = [UIColor whiteColor];
        NSArray *array1 = @[@"1",@"2",@"3",@"4",@"5"];
        NSArray *array2 = @[@"a",@"b",@"c",@"d",@"e",@"f"];
        [self.muArr addObject:array1];
        [self.muArr addObject:array2];
        
        for (int i = 0; i < self.muArr.count; i++) {
            [self.boolArray addObject:@"no"];
        }
        [self.view addSubview:self.tableView];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - UITableViewDataSource and ITableViewDelegate
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.muArr.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //如果self.boolArray的内容为“yes”,返回self.muArr中某个数组的个数,否则返回2
        if ([self.boolArray[section] isEqualToString:@"yes"]) {
            return [[self.muArr objectAtIndex:section] count];
        } else {
            return 2;
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 45;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellID = @"cellID";
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        /*
        else {
            //如果不增加下面这个判断,tableview 再滑动后,会有重影,或者在被选中后会有重影
            NSArray *views = [cell subviews];
            for (UIView *obj in views) {
                if (obj.tag==1000 || obj.tag==2000) {
                    NSLog(@"cell 要删除的子画面是:%@",[obj class]);
                    [obj removeFromSuperview];
                }
            }
        }
        */
        //显示每个section的第一个
        if ([[self.muArr objectAtIndex:indexPath.section] count] > indexPath.row) {
            cell.textLabel.text = [[self.muArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }
        //每个section的indexPath.row == 1显示为“更多”
        if ([self.boolArray[indexPath.section] isEqualToString:@"no"] && indexPath.row == 1 &&
            [[self.muArr objectAtIndex:indexPath.section] count] != 2) {
            cell.textLabel.text = nil;
            cell.textLabel.text = @"更多";
        }
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:indexPath.section]];
    
        if ([self.boolArray[indexPath.section] isEqualToString:@"no"]) {
            self.boolArray[indexPath.section] = @"yes";
            cell.textLabel.text = [[self.muArr objectAtIndex:indexPath.section] objectAtIndex:1];
            for (NSInteger i = 2; i < [[self.muArr objectAtIndex:indexPath.section] count]; i++) {
                NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
                [array addObject:index];
            }
            [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
        }else{
            self.boolArray[indexPath.section] = @"no";
            cell.textLabel.text = @"更多";
            for (NSInteger i = 2; i < [[self.muArr objectAtIndex:indexPath.section] count]; i++) {
                NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
                [array addObject:index];
            }
            [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([[self.muArr objectAtIndex:indexPath.section] count] == 2) {
            return nil;
        }
        if ([self.boolArray[indexPath.section] isEqualToString:@"yes"]) {
            return indexPath;
        }else{
            if (indexPath.row == 1) {
                return indexPath;
            }else{
                return nil;
            }
        }
    }
    
    #pragma mark - Getter and Setter
    - (NSMutableArray *)muArr {
        if (!_muArr) {
            _muArr = [[NSMutableArray alloc] init];
        }
        return _muArr;
    }
    
    - (NSMutableArray *)boolArray {
        if (!_boolArray) {
            _boolArray = [[NSMutableArray alloc] init];
        }
        return _boolArray;
    }
    
    - (UITableView *)tableView {
        if (!_tableView) {
            _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
            _tableView.delegate = self;
            _tableView.dataSource = self;
        }
        return _tableView;
    }
    
    @end
  • 相关阅读:
    MyEclipse和Microsoft Visual Studio常用快捷键
    数据源与JNDI资源实现JSP数据库连接池实例
    Tomcat配置+JSP页面模板修改UTF-8
    jquery插件
    jsp地址栏传中文显示乱码解决方法
    Java Web Project自定义错误页面,log4j记录日志。
    [原创] Java JDBC连接数据库,反射创建实体类对象并赋值数据库行记录(支持存储过程)
    测试上传图片
    js获取日期:昨天今天和明天、后天
    Axure谷歌浏览器Chrome扩展程序下载及安装方法
  • 原文地址:https://www.cnblogs.com/DWdan/p/5102045.html
Copyright © 2011-2022 走看看