您的位置:首页 > 其它

记录cell自适应,以及cell内容动态添加问题.

2017-01-23 17:51 441 查看
由于之前做的都是蓝牙功能为主,tableView还真是没认真去研究.

然后在cell自适应这块花了点时间,找到资料原来系统已经有自适应的api 了,这就XXX…兜了个圈.

但是系统的限定是约束一定要自上而下布局好,约束有问题也是同样不能自适应.

我是把主代码放在了基类里面,代码如下:

-//———————————–cell自适应

pragma mark - UITableViewDelegate

-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath

{

NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];

if(height)

{

NSLog(@”cell height – %f”,height.floatValue);

return height.floatValue;

}

else

{

return 100;

}

}

(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

NSNumber *height = @(cell.frame.size.height);

[self.heightAtIndexPath setObject:height forKey:indexPath];

}

(NSMutableDictionary *)heightAtIndexPath

{

if (!_heightAtIndexPath) {

_heightAtIndexPath = [NSMutableDictionary dictionary];

}

return _heightAtIndexPath;

}

//———————————–

_heightAtIndexPath 这个是可变字典类型,是为了缓存cell的高度,这样需要的时候才加载,

不会导致卡顿现象.

由于是做翻译项目,用到了原文跟译文两个label,自然也有两个model,或许我有点多此一举,但是独立开发,这是我唯一能想到的,希望有大神指引.代码如下:

#pragma mark - 懒加载


- (NSMutableArray *)topTranslateArray {

if (_topTranslateArray == nil) {

_topTranslateArray = [NSMutableArray array];

NSString *string = @”Siri 让你能够利用语音来完成发送信息、安排会议、查看最新比分等更多事务。只要说出你想做的事,Siri 就能帮你办到。Siri 可以听懂你说的话、知晓你的心意,甚至还能有所回应。iOS 7 中的 Siri 拥有新外观、新声音和新功能。它的界面经过重新设计,以淡入视图浮现于任意屏幕画面的最上层。”;

//生成假数据,循环string 截取长度添加到数组里,

for (int i = 0; i < 9; i++) {

LSTopModel *topModel = [[LSTopModel alloc]init];

NSInteger index = (arc4random()%(string.length / 20)) * 20;

topModel.topStr = [string substringToIndex:MAX(20, index)];

[_topTranslateArray addObject:topModel];

}

}

return _topTranslateArray;

}

(NSMutableArray *)bottomTranslateArray {

if (_bottomTranslateArray == nil) {

_bottomTranslateArray = [NSMutableArray array];

NSString *string = @”Siri 让你能够利用语音来完成发送信息、安排会议、查看最新比分等更多事务。只要说出你想做的事,Siri 就能帮你办到。以淡入视图浮现于任意屏幕画面的最上层。”;

for (int i = 0 ; i < 9 ; i++) {

LSBottomModel *bottomModel = [[LSBottomModel alloc]init];

NSInteger index = (arc4random()%(string.length/20))*20;

bottomModel.bottomStr = [string substringFromIndex:MAX(20, index)];

[_bottomTranslateArray addObject:bottomModel];

}

}

return _bottomTranslateArray;

}

然后这个也是要动态添加跟删除,由于tableView是调用model 的值,所以要改model ,如下:

pragma mark - 点击键盘return 做操作

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

//数组要与模型交互,因为label的值是从模型里取的,所以赋值同时要给模式赋值

LSTopModel *topModel = [[LSTopModel alloc]init];

topModel.topStr = textField.text;

[_topTranslateArray addObject:topModel];

LSBottomModel *bottomModel = [[LSBottomModel alloc]init];
bottomModel.bottomStr = @"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试";
[_bottomTranslateArray addObject:bottomModel];

NSLog(@"textField.text-%@",textField.text);
[self.hTableView reloadData];
textField.clearsOnBeginEditing = YES;
textField.returnKeyType = UIReturnKeyDone;
[textField resignFirstResponder];
return YES;


}

基本就是这样了,本来想记一下自己使用runtime 的代码,但是用到的都是基本的关联传值,就不记录了.到时自己需要时候可以翻翻代码就行了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  布局
相关文章推荐