在不同的部分(storyboard)中设置不同的行高

4

我有一个表格视图,其中包含多个原型单元格,我在storyboard中设计了它们,但是我在高度方面遇到了问题,因为我的第一个单元格应该与第二个不同,以此类推...我为每个单元格都有不同的标识符,并且由于我在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;



}

感谢您抽出时间。

1个回答

7

看起来你试图使用这个方法来达到它没有设计的目的...你需要重写该方法:

-(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,它是一个很好的资源。


1
谢谢!Bandokal,问题终于解决了。我没有提到,但它还给我带来了另一个问题(现在也解决了!),每次我进入编辑模式(我有多个分组部分)并想要另一行(按小绿加号按钮),行的内容都会混乱...我的意思是新行(第一部分743.0f的副本)被创建后,会停留在第二部分的顶部...现在一切都好了,再次感谢您。 - Japa
1
欢迎来到 Stack Overflow (SO) Japa!由于 BandoKal 回答了您的问题,请务必在他的回答旁边点击复选标记,以便给予他帮助您的功劳!此外,一旦您获得了10分声望,您也可以通过点击上箭头来投票赞成所有有帮助的答案,这也会为花时间帮助您的人赢得声望! - lnafziger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接