1

我有问题,我的应用程序崩溃了,cellForRowAtIdexPath因为我想为 2 个不同的行添加 2 个不同的自定义表格视图单元格。

看我的代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"MatchDetail";
MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

static NSString *CellIdentifier2 = @"GoalsList";
GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

if(indexPath.section == 0)
{
    if(indexPath.row == 0)
    {
        if (cell1 == nil) {
            cell1 = (MatchDetailsCell *)[MatchDetailsCell cellFromNibNamed:@"MatchDetailsCell"];
        }
        [cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
        return cell1;
    }
    else if (indexPath.row == 1)
    {
        if (cell2 == nil) {
            cell2 = (GoalsListCell *) [GoalsListCell cellFromNibNamed:@"GoalsListCell"];
        }
        [cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
        return cell2;
    }
}

return nil;

}

4

3 回答 3

2

您的代码不正确。您必须使用此标识符将该行的单元格出列。如果表中只有一个部分,则不需要进行 indexPath.section 检查。尝试这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier1 = @"MatchDetail";
    static NSString *CellIdentifier2 = @"GoalsList";
    if(indexPath.row == 0) {
         //The first row is the MatchDetailsCell
         MatchDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
         if(cell == nil) {
            //If cell not exists, you must create a new one
            cell = [[MatchDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
         }
         //Rest of your cell code here
         return cell;
    } else {
         //Rest of the cells are GoalsListCell
         GoalsListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
         if(cell == nil) {
             cell = [[GoalsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
         }
         //Rest of your cell code here
         return cell;
    }

}

我没有关于您的代码的更多信息,但也许它可以帮助您。

于 2014-08-13T11:28:48.010 回答
0

试试看,你不应该先对单元格进行出队,然后再从笔尖加载它。如果它在崩溃日志后不起作用。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"MatchDetail";
static NSString *CellIdentifier2 = @"GoalsList";

if(indexPath.section == 0)
{
    if(indexPath.row == 0)
    {
        MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        [cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
        return cell1;
    }
    else if (indexPath.row == 1)
    {
        GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        [cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
        return cell2;
    }
}

return nil;
于 2014-08-13T11:20:47.647 回答
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
       if ((indexPath.row%2)==0)   // for even rows, like 1,3,5,...
       {
         MyFirstCell *firstCell = (MyFirstCell *)[tableView dequeueReusableCellWithIdentifier:@"MyFirstCell"];

       if (firstCell == nil)
       {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyFirstCell" owner:self options:nil];
            firstCell = (MyFirstCell *)[nib objectAtIndex:0];
       }
        return firstCell;
     }
     else    // for even rows, like 2,4,6,...
     {
        MySecondCell *secondCell = (MySecondCell *)[tableView dequeueReusableCellWithIdentifier:@"MySecondCell"];

        if (secondCell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MySecondCell" owner:self options:nil];
            secondCell = (MySecondCell *)[nib objectAtIndex:0];
        } 
        return secondCell;
    }
    return nil;
}
于 2014-08-13T11:53:20.690 回答