uitableview标签不起作用?在一个uiviewcontroller中使用两个tableviews?

时间:2013-10-04 04:58:20

标签: iphone ios uitableview

我在一个视图控制器中使用两个表视图,两个表都有单独的数据。

当我实现tableview委托方法时,代码仅适用于tag1而不适用于tag2。

我已经在StackOverflow上使用了几乎所有可用的解决方案,但仍然面临这个问题。这是我的tableView:cellForRowAtIndexPath:

代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView.tag==1){
        
        static NSString *myIdentiFier=@"myIdentiFier";
        productCell *cell=(productCell *)[tableView dequeueReusableCellWithIdentifier:myIdentiFier];
        if(!cell){
            NSArray *cellObjs=[[NSBundle mainBundle]loadNibNamed:@"productCell" owner:self options:nil];
            for(id obj in cellObjs){
                if([obj isKindOfClass:[productCell class]]){
                    cell=(productCell *)obj;
                    break;
                }
            }
            cell.productTitle.text=[nameArray objectAtIndex:indexPath.row];
            [cell.productImage setImageWithURL:[NSURL URLWithString:[productImageArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"rsz_no-image"]];
        }
        
        return cell;
    }
    else{
        static NSString *mileIdentifier=@"mileIdentifier";
        mileStoneTableViewCell *mcell=(mileStoneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:mileIdentifier];
        if(!mcell){
            NSArray *cellObjs=[[NSBundle mainBundle]loadNibNamed:@"mileStoneTableViewCell" owner:self options:nil];
            for(id obj in cellObjs){
                if([obj isKindOfClass:[mileStoneTableViewCell class]]){
                    mcell=(mileStoneTableViewCell *)obj;
                    break;
                }
            }
            mcell.mile_description.text=[mileStoneDescr objectAtIndex:indexPath.row];
            
        }
        
        return mcell;
    }
}

2 个答案:

答案 0 :(得分:0)

保留对每个表视图的引用

// viewcontroller.h

@property (nonatomic, weak) IBOutlet UITableView* firstTableView;
@property (nonatomic, weak) IBOutlet UITableView* secondTableView;

在datasource / delegate方法中,您需要考虑到该方法需要根据正在使用的表视图而表现不同的事实。 e.g。

// viewcontroller.m

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    if (tableView == self.firstTableView) {

        ...

    } else { // tableView == self.secondTableView

        ...
    }
}

return cell;
}

答案 1 :(得分:0)

请注意以下事项:
 你打电话给[self.secondTableView reloadData]  2.尝试NSLog(@"%d", self.secondTableView.tag)以确保标记设置正确  3.您是否使用了界面构建器来设置标记,或者是否以编程方式设置了标记?如果您以编程方式设置了它,请确保在致电reloadData之前设置了标记  4.最后确保您的dataSourcedelegate设置正确  5.如果您有两个tableView,建议不要通过Interface Builder中的控件拖动来设置两个tableViews的dataSourceDelegate。在某些情况下,可能会导致应用程序崩溃。以编程方式设置tableView的datasourcedelegate中的一个或两个。

如果所有这些都是正确的,那么您的代码应该可以正常工作。