2016-07-25 26 views
2

两个表必须位于一个视图控制器中,例如通过段控制进行切换。数据是无限的,从核心数据中获取。我看到三种解决方案:如何更好地使用两个“选项卡”实现UITableView?

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 
    } 
} 
+1

第三个选择预订购是有用的。速度将与其他选项相同。 –

回答

5

选项数量third是容易的,良好的记忆优化也是,在我的建议,如果你选择使用tag概念与单一的tableView的任何细分为你的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 
} 
+0

Ty为答案。你能解释关于标签的更广泛的想法吗? –

+0

@ShadowOf - 你确定,你可以更新代码,这很容易解决和解决 –

+0

想法单个tableView作为关于标签1和2的两个不同视图? –

1

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

1
  1. 您需要为两个标签 2.Load的tableview当标签更改创建两个数据源阵列。
  2. 在cellforrowatindexpath中,您需要处理来自选定选项卡的数据源数组的显示数据。

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

1

我认为,处理一个表视图与两个数组取决于分段控制也许是最好的选择。我将添加一个将处理哪个段索引的bool,例如isZero。只需创建一个UISegmentedControl并处理索引操作。注意:我将添加Objective-C代码,但我认为您会认识到概念。因为使2个表意味着存储器消耗更好relaod单table..By双端队列可重复使用的存储器将被更好地利用

-(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; 
} 
+1

Obj-C代码与Swift代码一样受欢迎; ty为答案! –

相关问题