2015-09-04 77 views
1

我正尝试创建带有双NSMutableArray值的单个table view。现在我保持UISegmentcontrol第一个按钮点击加载第一个NSMutableArray数据。第二个按钮点击加载第二个NSMutableArray数据值(点击第二个按钮后,第一个数据不应该显示)。如何将tableview数据重新加载到另一个数组?

NSMUtableArray *firstarray = [First values]; 
NSMUtableArray *secondarray = [second values]; 


-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment 
{ 
    switch (segment.selectedSegmentIndex) { 
     case 0:{ 
      //action for the first button (Current) 
      //want to load first array data into table 
      break;} 
     case 1:{ 
      //action for the first button (Current) 
      //want to load second array data into table 
      break;} 
    } 
} 

回答

4

创建1个数组来保持表中的数据,所以你不必在UITableView中的每一个委托方法来检查(使用if ...):

NSMutableArray * arrTableData; 

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment 
{ 
    switch (segment.selectedSegmentIndex) { 
     case 0:{ 
      arrTableData = firstarray; 
      [tableView reloadData]; 

      break;} 
     case 1:{ 
      arrTableData = secondarray; 
      [tableView reloadData]; 

      break;} 
    } 
} 

使用arrTableDataUITableView代表:numberOfRowsInSectioncellAtIndex ...

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

    return arrTableData.count; 

} 
+0

但第一次 我做的事?在选择按钮之前,我需要列出默认的第一阵列数据@ anthu – Anushka

+0

是的,您可以通过在某处指定arrTableData = firstarray来选择默认值。 – anhtu

+0

当你设置选定的段你也可以设置数组的初始值 – Wain

2

您可以使用枚举来检查您按下的按钮。对于按钮1,将枚举值设置为1,对于按钮2,将枚举值设置为2.在表格视图中,可以检查当前选定的枚举并返回适当的值。

例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
NSInteger count = 0; 


if(m_choice == 1) 
    count = [firstarray count]; 
else if(m_choice == 2) 
    count = [secondarray count]; 

return count;} 

而且这样做是为了其他表视图方法了。

+0

虽然这会工作这是一个很大重复的代码和其他答案是更优雅的方法 – Wain

1

实现你UITableViewDelegate在这个下列方式

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

    if (mySegmentContorll.selectedSegmentIndex == 0) { 
     // Draw Cell content with data taken from first array, data = firstarray[indexPath] 
    } else if (mySegmentContorll.selectedSegmentIndex == 1) { 
     // Draw Cell content with data taken from second array, data = secondarray[indexPath] 
    } 

} 

当分段控制更改时,还请致电[tableView reloadData];

1

创建一个布尔变量说isLoadFirst 然后在下面的代码

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment 
{ 
switch (segment.selectedSegmentIndex) { 
    case 0:{ 
     isFirstLoad = YES; 
     [yourTableView reloadData]; 
     //action for the first button (Current) 
     //want to load first array data into table 
     break;} 
    case 1:{ 
     //action for the first button (Current) 
     //want to load second array data into table 
     isFirstLoad = NO; 
     [yourTableView reloadData]; 
     break;} 
} 
} 

现在,在您

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (isFirstLoad) 
    return firstArrayCount; 
    else 
    return secondArrayCount 
} 

而且同一支票

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
+0

这里最精通的答案 – iAhmed

+0

谢谢Arsian。它也很好的答案! – Anushka

相关问题