表视图单元数据加载

时间:2016-08-06 12:03:01

标签: ios uitableview lazy-loading

我必须创建一个模块,我必须在其中显示用户的数据。为此我有两个网络服务。第一个Web服务为每行加载数据提供带有url的行数。例如,第一个Web服务返回json,如

{
    "0": {
       "name":"Personal Info",
       "url":"http://www.google.com/profile"
    },
    "1": {
       "name":"Contact Info",
       "url":"http://www.google.com/contact"
    }
}  

现在我必须在Table View单元格上加载每个url数据,Cell高度将是动态的。能不能给我建议

3 个答案:

答案 0 :(得分:1)

您需要做的是在单元格中取一个标签,并从属性检查器中将其行数设置为0。从顶部,前导,尾随和底部(相对于单元格)设置其约束。不要修理它的高度。在您的cellForRowAtIndexPath中设置数据后,在获取并将数据设置为label.text之后添加以下行: -

self.tableView.estimatedRowHeight = 140

这将在标签上设置获取的信息(根据文本增加其大小),并且您的单元格也会相应地调整自身大小。 还要添加此方法: -

{{1}}

并在viewDidLoad()方法中给出一些估计高度,如下所示: -

{{1}}

答案 1 :(得分:0)

使用基本类型的uitableview单元格,然后将其标签集行号设置为0,并调用下面的动态单元格高度方法。

  override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       return UITableViewAutomaticDimension
        }

答案 2 :(得分:0)

如果要为每一行设置不同的高度,则应使用UIViewDelegate协议中的此方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

这是一个使用它的示例,ViewController.m的代码:

#import "ViewController.h"
#import "MyTableCellTableViewCell.h"

@interface ViewController ()

@property NSMutableArray* dataArray;
@property UIFont* cellTextFont;

@end

NSString* cellID = @"MyTableViewCell";

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_cellTextFont = [UIFont systemFontOfSize:15];

_dataArray = [[NSMutableArray alloc] initWithObjects:
              @"Hello world!",
              @"Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
              @"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
              nil];

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.dataSource = self;
self.tableView.delegate = self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//Set different row height by Odd and even
//    if (0 == indexPath.row % 2) {
//        return 30;
//    }
//    else
//        return 50;

//Set different row height by content text's height
NSString* contentStr = [_dataArray objectAtIndex:indexPath.row];
float textWidth = self.tableView.frame.size.width;
NSDictionary *attributes = @{NSFontAttributeName: _cellTextFont};
CGRect labelSize = [contentStr boundingRectWithSize:CGSizeMake(textWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
return labelSize.size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (nil == cell) {
    cell = [[MyTableCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.lbContent.font = _cellTextFont;
}
cell.lbContent.text = [_dataArray objectAtIndex:indexPath.row];
return cell;
}

@end

这是MyTableCellTableViewCell.h

#import <UIKit/UIKit.h>

@interface MyTableCellTableViewCell : UITableViewCell

@property UILabel* lbContent;

@end

而且,这是MyTableCellTableViewCell.m:

@interface MyTableCellTableViewCell()

@property UIView* separation;

@end

@implementation MyTableCellTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

_lbContent = [[UILabel alloc] init];
_lbContent.numberOfLines = INT_MAX;
[self addSubview:_lbContent];

_separation = [[UIView alloc] init];
_separation.backgroundColor = [UIColor blackColor];
[self addSubview:_separation];

return self;
}

-(void)layoutSubviews{
float separationHeight = 1;
_lbContent.frame = self.bounds;
_separation.frame = CGRectMake(0, self.bounds.size.height-separationHeight, self.bounds.size.width, separationHeight);
}

@end

希望它可以帮到你。

相关问题