本文來源于作者實際開發(fā)中的經(jīng)驗朴乖,分享出來祖屏,希望能給被同樣問題困擾的同道帶來幫助;
使用xib注冊cell买羞,有時需要動態(tài)改變cell的高度袁勺,但是如果你如下代碼進行操作,xcode可能就會報一些奇怪的錯誤;
為tableView注冊cell:
[tableView registerNib:[UINib nibWithNibName:@"xxxCell" bundle:nil] forCellReuseIdentifier:@"cell"];
在代理cellForRowAtIndexPath:(NSIndexPath *)indexPath中對cell進行復(fù)用設(shè)置;
錯誤示例:
xxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
上述復(fù)用cell時畜普,加了forIndexPath:indexPath調(diào)用下列代理時就會有問題:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
正確示例:
xxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
這樣代理方法heightForRowAtIndexPath:(NSIndexPath *)indexPath可以正常發(fā)揮作用期丰,編譯也不會報錯;
原理:略
如有幫助,請點關(guān)注钝荡!
荊軻刺秦王街立!