我有问题,我的应用程序在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;
}
答案 0 :(得分: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;
}
}
我没有关于您的代码的更多信息,但可能对您有帮助。
答案 1 :(得分:0)
试试这个,你不应该先破坏单元格,然后从nib加载它。 如果它在崩溃日志后不起作用。
-(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;
答案 2 :(得分: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;
}