基础知识iOS UI开发

Cell的折叠和展开(主要针对Cell上的label文字内容)

2016-11-05  本文已影响413人  HuangLinWang
demo.gif

思路

      //预估行高
      self.tableView.estimatedRowHeight = 100;
      //指定计算行高
      self.tableView.rowHeight = UITableViewAutomaticDimension;
        // 多行
        label.numberOfLines = 0;
        // 1行
        label.numberOfLines = 1;
        -(void)clickBtn{
            
            if (self.contentL.numberOfLines == 1) {
                self.model.numberOfLine = 0;
            }else{
                self.model.numberOfLine = 1;
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"XXXX" object:self];
        }
        // 监测通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lwReloadData:) name:@"XXXX" object:nil];
        -(void)lwReloadData:(NSNotification *)noti{            
            LWTableViewCell *cell = [noti object];            
            NSIndexPath *index = [self.tableView indexPathForCell:cell];            
            [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];            
        }
      -(void)setModel:(LWModel *)model{
            _model = model;
            self.icon.image = [UIImage imageNamed:model.icon];
            
            self.nameL.text = model.name;
            
            self.contentL.text = model.intro;
            // 对Cell的内容属性的label行数进行赋值
            self.contentL.numberOfLines = model.numberOfLine;
        }
        -(void)loadData{
            
            NSData *data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"MessageSource.json" withExtension:nil]];
            
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            NSMutableArray *arrM = [NSMutableArray array];
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                LWModel *model  = [LWModel lwModelWith:obj];
                // 设置为1 是为了让label默认显示1行
                model.numberOfLine = 1;
                [arrM addObject:model];
            }];
            _dataList = arrM.copy;
        } 
上一篇下一篇

猜你喜欢

热点阅读