我遇到了问题,我的应用程序崩溃了,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;
}
您的代码不正确。您必须使用此标识符使该行的单元出队。如果表中只有一个部分,则无需进行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;
}
}
我没有关于您的代码的更多信息,但也许可以为您提供帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句