2015-09-04 92 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

相關問題