-2

我认为我有两个表视图。我在实现委托方法时遇到问题。谁可以帮我这个事。这里的问题是,这个方法需要一个 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;

}
}
4

2 回答 2

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; 
    }
于 2013-06-17T11:21:09.687 回答
1

无需强制转换ContactListCustomCellUITableViewCell. 您可以将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;
}

祝你好运 !!!

于 2013-06-17T11:23:03.730 回答