我有一个表格 View ,其中包含我在 Storyboard中设计的多个原型(prototype)单元格,但我遇到了高度问题,因为我的第一个单元格应该与第二个单元格不同,依此类推...我有不同的标识符对于每个单元格,因为我在 Storyboard中设计了它们,所以我知道它们的高度。我的代码中有这个,但它不起作用,有人知道如何修复它吗?:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]init];
switch (indexPath.section)
{
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
return 743.0f;
break;
case 2:
cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
return 300.0f;
}
}
感谢您的宝贵时间。
最佳答案
看起来您正在尝试将此方法用于其设计目的之外的目的... 您需要重写该方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
case 1:
static NSString *CellIdentifier = @"cell1";
break;
case 2:
static NSString *CellIdentifier = @"cell2";
break;
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
return cell;
}
仅更改 heightForRowAtIndexPath 中的行高:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
{
case 1:
return 743.0f;
break; //technically never used
case 2:
return 300.0f;
}
查看本教程 http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1这是一个很好的资源
关于uitableview - 在不同部分( Storyboard)设置不同的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488805/