【问题标题】:IOS: tableview delegate methods for two tableviewIOS:两个tableview的tableview委托方法
【发布时间】:2011-06-29 11:11:11
【问题描述】:

我在一个类中为 tableview 提供了这些委托方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [array1 count];
}

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


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
}

cell.textLabel.text = [array1 objectAtIndex:indexPath.row];

return cell;
}

如果我有一个 UITableView 没关系,但如果我有两个 UITableView?如何组织我的代码?带标签?

【问题讨论】:

    标签: ios xcode uitableview delegates


    【解决方案1】:

    查看所有委托方法中如何包含tableView:(UITableView *)tableView

    你可以在头文件中定义你的表视图,然后简单地去:(假设你的表被称为myTable

    if (tableView == myTable)
    

    然后,您可以拥有任意数量的表格视图。

    例如:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [array1 count];
    }
    

    变成:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView == myTable)
        {
           return [array1 count];
        }
        if (tableView == myTable2)
        {
           return [array2 count];
        }
    
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      我的建议是让您的数据源充当表格视图委托,而不是您的控制器。

      这是一种更接近模型-视图-控制器模式的设计,可以让您获得更大的灵活性,并避免检查 tableView 参数在您的委托方法中获得的特定值。

      在您的情况下,您的委托/数据源将是一个具有NSArray 类型成员并且还实现UITableViewDelegate 协议的类。

      【讨论】:

        【解决方案3】:

        是的,你可以用标签来做到这一点。给你的 UITableViews 标签 1 和 2。

        设置开关:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
        }
        
        switch ([tableView tag]) {
          case 1:{
            [[cell textLabel] setText:@"First tag"]
            break;
          }
          case 2:{
            [[cell textLabel] setText:@"Second tag"]
            break;
          }
          default:
            break;
        }
        
        return cell;
        }
        

        【讨论】:

        • 标签的使用很好,处理多个表格更容易
        【解决方案4】:

        这些方法中的每一个都传入对调用它的表视图的引用。我通常将每个表连接到接口构建器中的插座,并根据与委托方法中的 tableView 和插座名称的比较有条件地返回数据源。使用标签也可以这样做,但在编辑视图结构时会更混乱且更容易出现复杂情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-30
          • 1970-01-01
          • 1970-01-01
          • 2018-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多