返回CellForRowAtIndexPath的类型

时间:2013-06-17 11:17:03

标签: iphone ios uitableview delegates

我的观点中有两个表格视图。我在实现委托方法时遇到了问题。谁可以帮我这个事。这里的问题是,这个方法需要UITableViewCell类型的返回值。我该怎么回事?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:@"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;

return cell;
}

if (tableView==ContactTableViewLabel)
{
    static NSString *CellIdentifier = @"ContactListCustomCell";

    ContactListCustomCell *cell = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContactListCustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (ContactListCustomCell *) currentObject;
                break;
            }
        }
    }

    cell.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
    cell.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];

    return cell;

}
}

2 个答案:

答案 0 :(得分:3)

使用此编辑的代码。它还可以解除有关返回单元格的任何警告。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
     static NSString *CellIdentifier_contact = @"ContactListCustomCell";
    UITableViewCell *cell;
    if(tableView==ContactTableview)
    {


    if (cell == nil) {
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
    NSArray *afterSeparate = [[NSArray alloc]init];
    afterSeparate = [totalString componentsSeparatedByString:@"+"];
    NSString *cellText = [afterSeparate objectAtIndex:0];
    //NSString *detailText = [afterSeprate objectAtIndex:1];
    cell.textLabel.text = cellText;
    //cell.detailTextLabel.text=detailText;


    }

    if (tableView==ContactTableViewLabel)
    {


        ContactListCustomCell *cell1 = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell1 == nil) {

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContactListCustomCell" owner:self options:nil];

            for (id currentObject in topLevelObjects){
                if ([currentObject isKindOfClass:[UITableViewCell class]]){
                    cell1 =  (ContactListCustomCell *) currentObject;
                    break;
                }
            }
        }

        cell1.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
        cell1.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];

        cell = (UITableViewCell*) cell1;

    }
   return cell; 
    }

答案 1 :(得分:1)

无需将ContactListCustomCell转换为UITableViewCell。您可以使用ContactListCustomCell作为UITableViewCell的子类。您可以像这样更新代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if(tableView==ContactTableview)
    {
        static NSString *CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        .
        .
    }
    else if(tableView==ContactTableViewLabel)
    {
        static NSString *CellIdentifier = @"CustomCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            [[NSBundle mainBundle] loadNibNamed:@"yourCellNibName" owner:self options:nil];
            cell = self.yourCellProperty;
            self.yourCellProperty = nil;
        }
        .
        .
    }
    return cell;
}

GoodLuck !!!