主要有两行代码,分别写在UITableView的两个代理方法里面:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath(显示单元格的内容)
- (float )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath(定制单元高度-所有单元格的高度都一样)
参考代码如下:
[代码]c#/cpp/oc代码:
01 | 设置单元格内容的代理方法 |
02 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
03 | { |
04 | // |
05 | static NSString *cellStr = @"cell" ; |
06 | //实例一个myCell类的对象cell。 |
07 | // |
08 | CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr]; |
09 | // |
10 | if (cell == nil) |
11 | { |
12 | cell = [[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr]; |
13 | } |
14 | |
15 | //获得当前评论内容的高度,赋值给cell,之后定制单元格高度的时候我们只要用到cell的高,其它参数设为0对单元格的定制是不会有影响的 |
16 | [cell setFrame:CGRectMake(0, 0, 0, self.cellAddHight)]; |
17 |
18 | return cell; |
19 | } |
[代码]c#/cpp/oc代码:
1 | - ( float )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath |
2 | { |
3 | // 通过indexPath确定定制高度的是第几行 |
4 | UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; |
5 | // 返回自适应高度 |
6 | return 99+cell.frame.size.height; |
7 | } |