Reputation: 407
I have added a segment control to my table view and what my problem/question is I need to display text in tableview cells based on segment selected.
I have followed some tutorials, in that they gave me text in labels based on segment selected. I used segmentControl.selectedIndexPath
also. So can anyone tell me how can we set that array of objects to my tableview cells based on segment selected?
Correct me if any mistakes in my english.
Please help me. Thanks a lot for help.
Upvotes: 1
Views: 1659
Reputation: 5267
First of all you have to divide your data in different arrays depends on your requirement ie Number of segmentControl.
Here if there are three segment controls then create three array an in the table view's delegate methods depending on segment control's selected index change the array to display in table view.
Like if segmentControl.selectedIndex == 0
then array1
if == 1
then array2
and if == 2
then array3
.
In all delegate and datasource methods of table view. And on segment control's selecedIndexChange: method call reload table.
Happy Coding :)
EDIT 1
For change data in table view on segmented control's index change you must have one IBOutlet
for tableView and use that IBOutlet
to change the data using [tableView reloadData];
here tableView
is IBOutlet
for table view.
Happy Coding :)
Upvotes: 2
Reputation: 6065
Common use for tableview; In Controller you have an array which has the objects, lets say _tableObjects You have implemented in your controller datasource methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =......
.................
cell.labelText.text = [_tableObjects objectAtIndex:indexPath.row].name;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableObjects.count;
}
So if it is like this, you can change _tableObjects array in somewhere in you code.
_tableObjects = myOtherArray;
[self.tableView reloadData];
Upvotes: 1