【问题标题】:How better implement UITableView with two "tabs"?如何更好地使用两个“选项卡”实现 UITableView?
【发布时间】:2026-02-22 09:50:01
【问题描述】:

两个表必须在一个视图控制器中,例如通过段控制切换。数据是无限的,从核心数据中获取。我看到了三个解决方案:

1) 创建UITableView的两个对象,并将它们填充到共享数据源函数中;隐藏其中一个表视图

2) 创建两个视图控制器容器,嵌入到主视图控制器中,执行完全分离的数据源方法;隐藏其中一个容器

3) 仅使用一个表视图对象,在需要时重新加载数据,提供偏移量保存

需要您的意见。哪种解决方案会更快和/或更易读且正确?

更新

让我尝试实现第三个选项:

var tableViewOffsets = [Int: CGPoint]()

func segmentValueChanged(sender: UISegmentedControl) {
    tableViewOffsets[tableView.tag] = tableView.contentOffset
    tableView.tag = sender.selectedSegmentIndex
    tableView.reloadData()
    if let savedOffset = tableViewOffsets[tableView.tag] {
        tableView.setContentOffset(savedOffset, animated: false)
    }
}

func tableView_dataSourceMethodsTemplate(tableView: UITableView, ...) {
    if tableView.tag == 0 {
        //perform data source code for first tab
    } else {
        //perform data source code for second tab
    }
}

【问题讨论】:

  • 第三个选项将很有用,因为制作两个表意味着内存消耗更好地重新加载单个表。通过双端队列可重用内存将得到更好的利用。和速度将与其他选项相同。

标签: ios objective-c swift uitableview


【解决方案1】:

选项编号third 也很容易和良好的内存优化,在我的建议中,如果您选择了任何段,请使用带有单个 tableView 的 tag 概念,为您的 tableview 分配标签(例如 yourtableview.tag = 1)并在同时改变你需要的框架,

更新

func segmentValueChanged(sender: UISegmentedControl) {
tableViewOffsets[tableView.tag] = tableView.contentOffset
tableView.tag = sender.selectedSegmentIndex
var tablearray = [string]()
 if sender.selectedSegmentIndex ==  0
{
  // here fecth the value in coredata and append in array
 tablearray.append (yourdta)
 }else
  {
  // here fecth the value in coredata and append in array
    tablearray.append (yourdta)
  }


if let savedOffset = tableViewOffsets[tableView.tag] {
    tableView.setContentOffset(savedOffset, animated: false)
}
tableView.reloadData()
}

func tableView_dataSourceMethodsTemplate(tableView: UITableView, ...)    {
 return tablearray.count
}

【讨论】:

  • 回答问题。你能解释一下关于标签的更广泛的想法吗?
  • @ShadowOf - 是的,你能更新代码吗,很容易继续解决
  • 将单个 tableView 视为带有标签 1 和 2 的两个不同视图的想法?
  • @ShadowOf - 在这里我们可以考虑多种方式,一个滚动视图(两个表格视图更改内容偏移量,我们根据选择更改框架)。第二个带有动画的简单 UIView 我们可以重新加载表格(使用标签)
  • @ShadowOf - 你有想法或者你需要任何额外的帮助
【解决方案2】:

您可以选择第三种解决方案。您可以使用 UISegmentedControl 作为您的选项卡,在选择段时,您可以将数据源分配给同一个 tableView。这也将减少内存使用,因为您将使用单个 tableView 并且它将被预加载以用作第二个 tableView .

【讨论】:

    【解决方案3】:
    1. 您需要为两个选项卡创建两个数据源数组 2.tab改变时加载tableview。
    2. 在 cellforrowatindexpath 中,您需要处理所选选项卡的数据源数组中的显示数据。

    即。您可以使用单个表格视图处理两个选项卡。

    【讨论】:

      【解决方案4】:

      我认为根据分段控制处理一个带有两个数组的表格视图可能是最好的选择。我会添加一个布尔值来处理哪个段索引,例如 isZero。只需创建一个UISegmentedControl 和用于处理索引的操作。注意:我会添加objective-c代码,但我想你会认识概念。

      -(IBAction)segmentChangeViewValueChanged:(UISegmentedControl *)segmentedControl
      {
          if (segmentedControl.selectedSegmentIndex == 0) {
              self.headerLabel.text = @"Contacts";
              self.searchButton.hidden = YES;
              self.isContactsSegment = YES;
              self.searchTextField.text = @"";
              self.refinedContactsSearch = [NSMutableArray arrayWithArray:self.contacts];
              [self.contactsTableView reloadData];
          }
      
          else {
              self.headerLabel.text = @"Search on server";
              self.searchButton.hidden = NO;
              self.isContactsSegment = NO;
              self.searchTextField.text = @"";
              [self.contactsTableView reloadData];
          }
      
      
      }
      
      
      cellForRow:
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          ContactsTableViewCell *contactCell = (ContactsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"contactCell"];
          ContactsModel *model = [[ContactsModel alloc] init];
          if (self.isContactsSegment == YES) {
              model = self.refinedContactsSearch[indexPath.row];
          }
          else {
              model = self.serverContacts[indexPath.row];
          }
      
          if (contactCell == nil) {
              contactCell = [[OTContactsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"contactCell"];
              contactCell.backgroundColor = [UIColor clearColor];
              [contactCell.contactLabel setTextColor:NEON_GREEN];
              [contactCell setSelectionStyle:UITableViewCellSelectionStyleNone];
              [contactCell.separator setBackgroundColor:[self.contactsTableView separatorColor]];
          }
          contactCell.contactLabel.text = [NSString stringWithFormat:@"%@ %@", model.name, model.surname];
          [contactCell.contactLabel sizeToFit];
          [contactCell.contactImage setImageWithURL:[NSURL URLWithString:model.pictureUrl] placeholderImage:[UIImage imageNamed:@"Logo"]];
          contactCell.contactImage.layer.cornerRadius = contactCell.contactImage.frame.size.height/2;
          contactCell.contactImage.layer.masksToBounds = YES;
      
      
      
          return contactCell;
      }
      

      【讨论】: