0

我有两个表(单元标识符:呼叫和单元标识符:Cell2)。我正在传递两个对象数组(每个表一个)但是当我在我的 tableView 中执行此操作时,我的第二个表会显示与我的第一个表相同的数据,而不是来自第二个数组的数据。我的数组在我的 .h 文件中全局设置为 NSMutableArray。这是我认为问题出在代码中的地方:

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

   // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added
   //  NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added


    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    label1.text = [arrayDate1 objectAtIndex:indexPath.row];
    label2.text = [arrayDate2 objectAtIndex:indexPath.row];

   // cell.textLabel.text = [NSString stringWithFormat:@"%@  %@", currDate, someOtherKey]; //added

    return cell;



    //This will Load the second table (myTableView2)
    static NSString *CellIdentifier2 = @"Cell2";


    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if(!cell2)
    {
        cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
    }

    // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added
    //  NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added


    cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];


    return cell2;
}
4

1 回答 1

1

这个函数确实有问题:

编写的代码将简单地运行直到它到达第一return cell;行,之后不再运行代码,因此总是返回一个Cell单元格的实例。

为了像这样使用两个表,您需要能够将它们区分开来。通常,您将它们都存储在声明的属性中。对于这个答案的其余部分,我将假设您正在这样做并且它们被称为table1and table2

将您的代码更改为如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.table1]) {
        static NSString *CellIdentifier = @"Cell";


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(!cell)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

       // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added
       //  NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added


        cell.textLabel.text = [array objectAtIndex:indexPath.row];
        label1.text = [arrayDate1 objectAtIndex:indexPath.row];
        label2.text = [arrayDate2 objectAtIndex:indexPath.row];

       // cell.textLabel.text = [NSString stringWithFormat:@"%@  %@", currDate, someOtherKey]; //added

        return cell;
    } else if ([tableView isEqual:self.table2]) {


        //This will Load the second table (myTableView2)
        static NSString *CellIdentifier2 = @"Cell2";


        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if(!cell2)
        {
            cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        }

        // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added
        //  NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added


        cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];


        return cell2;
    }

}

// If you reach this point, we don't recognize the table and return `nil`, then the program will crash.  Handle this however you want.
return nil;
于 2012-11-19T00:30:28.993 回答