从cellForRowAtIndexPath返回两个不同的单元格

时间:2014-04-16 10:16:51

标签: ios objective-c uitableview

如何启动我的单元格以cellForRowAtIndexPath委托方式返回?

假设有一个var i,我可以是0或1; 这样:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id cell=nil;
switch (i) {
    case 0:{
        static NSString *CellIdentifier=@"RecommendTableCellIdentifier";
        RecommendTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
            cell=[[RecommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        dataRow=[dataList objectAtIndex:[indexPath row]];
        [cell addContent:dataRow];
        [cell.wantToSeeButton setTag:[indexPath row]];
        [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        break;
    case 1:{
        static NSString *CellIdentifier=@"WantTableCellIdentifier";
        WantTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
            cell=[[WantTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        dataRow=[dataList objectAtIndex:[indexPath row]];
        [cell addContent:dataRow];
        [cell.wantToSeeButton setTag:[indexPath row]];
        [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        break;

    default:
        break;
}
return cell;

}

CustomCellA和B都使用Class和xib定义。

我怎样才能实现这一目标? 非常感谢!

2 个答案:

答案 0 :(得分:3)

这样做:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id cellToReturn=nil;
switch (i) {
    case 0:{
        static NSString *CellIdentifier=@"RecommendTableCellIdentifier";
        RecommendTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
            cell=[[RecommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        dataRow=[dataList objectAtIndex:[indexPath row]];
        [cell addContent:dataRow];
        [cell.wantToSeeButton setTag:[indexPath row]];
        [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        cellToReturn=cell;
        break;
    case 1:{
        static NSString *CellIdentifier=@"WantTableCellIdentifier";
        WantTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
            cell=[[WantTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        dataRow=[dataList objectAtIndex:[indexPath row]];
        [cell addContent:dataRow];
        [cell.wantToSeeButton setTag:[indexPath row]];
        [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        cellToReturn=cell;
        break;

    default:
        break;
}

return cellToReturn;

}

希望这有助于......:)

答案 1 :(得分:0)

我的理解是你想避免代码重复?试试这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id cell=nil;

switch (i) {
    case 0:{
        static NSString *CellIdentifier=@"RecommendTableCellIdentifier";
        cell=(RecommendTableCell  *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
        break;
    case 1:{
        static NSString *CellIdentifier=@"WantTableCellIdentifier";
        cell = (WantTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
        break;

    default:
        break;
}
dataRow=[dataList objectAtIndex:[indexPath row]];
[cell addContent:dataRow];
[cell.wantToSeeButton setTag:[indexPath row]];
[cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside];

return cell;
}