
怎么计算一个tableview显示所有的cell需要的高度
1个回答
展开全部
一:改变它的加载顺序,或者说白了就是计算好cell高度后,再次让它加载heightForRowAtIndexPath方法; 二:直接在heightForRowAtIndexPath计算,做判断,直接返回对应的高度。 以下是第一种方法的实例: UITableView设置单元格的高度的方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return64;} // Somewhere in your header: NSIndexPath *selectedCellIndexPath; // And in the implementation file:-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedCellIndexPath = indexPath;//Forces the table view to call heightForRowAtIndexPath[tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {//Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]//will call heightForRow and thus create a stack overflow if(selectedCellIndexPath !=nil && [selectedCellIndexPath compare:indexPath] == NSOrderedSame){return128;}else{return64;}}reloadRowsAtIndexPaths方法将重新调用heightForRowAtIndexPath使单元格改变高度。 reloadRowsAtIndexPaths是在3.0.存储NSIndexPath的原因是因为不可能在堆栈不溢出的情况下在
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询