【问题标题】:uitableview tags are not working ? Using two tableviews in one uiviewcontroller?uitableview 标签不起作用?在一个 uiviewcontroller 中使用两个表视图?
【发布时间】:2013-10-04 13:43:42
【问题描述】:

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

当我实现 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;
    }
}

【问题讨论】:

  • 您是否为第二张表设置了数据源和委托?检查一次。
  • 您是否也为第二张表设置了 tag = 2 ?
  • 我为我的第一个 tableview 设置了 tag=1,为第二个 tableview 设置了 tag=2。
  • Else 部分不工作或者甚至没有进入 else 部分?
  • 它在我第二次运行应用程序后进入其他部分。

标签: iphone ios uitableview


【解决方案1】:

保留对每个表视图的引用

//viewcontroller.h

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

在 datasource/delegate 方法中,您需要考虑一个事实,即该方法需要根据所使用的 table view 表现出不同的行为。例如

//viewcontroller.m

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

    ...

    if (tableView == self.firstTableView) {

        ...

    } else { // tableView == self.secondTableView

        ...
    }
}

return cell;
}

【讨论】:

  • 你在xib中连接了两个table view的插座吗?
  • 它不正常吗?您在 numberofRos 部分中提到过这种情况吗?
  • 两个表都设置了什么标签?
  • 实际上两个tableview都没有同时获取数据..两者都在连续获取数据。一个tableview在第一次运行时获取数据,另一个在第二次运行应用程序时获取数据。
  • ok 为 tableview1 设置标签 1 并在 xib 中为 tableview 2 设置标签 2 然后你必须检查 if (tableView.tag == 1) 这样的条件而不是 if (tableView == self.firstTableView) {} else if (tableView.tag == 2) {
【解决方案2】:

请注意以下几点:
1.你有没有打电话给[self.secondTableView reloadData].
2. 尝试NSLog(@"%d", self.secondTableView.tag) 确保标签设置正确。
3. 您是使用界面生成器设置标签还是以编程方式设置?如果您以编程方式设置了它,请确保在调用 reloadData 之前设置了标签。
4. 最后确保您的dataSource 和delegate 设置正确。
5.如果你有两个tableView,建议不要在Interface Builder中通过控件拖动来设置两个tableView的dataSource和Delegate。在某些情况下,这可能会导致应用程序崩溃。以编程方式设置 tableView 的 datasource 和 delegate 之一或两者。

如果这一切都是正确的,你的代码应该可以工作。

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    相关资源
    最近更新 更多