I am using a segmented control with titles, phone and email. I am fetching the contacts details from addressbook and storing them as array of dictionaries. Each dictionary is with keys "name", "email", "image", "phone". My requirement is to show only contacts with emails in tableview when email is tapped and show contacts with phones when phone button is tapped on segment control. Please help me to achieve this.
Asked
Active
Viewed 836 times
0
-
you were store teh all dictionary in single array. at the same time show your segment control action , it is easy to resolve – Anbu.Karthik Mar 03 '16 at 13:03
2 Answers
1
we can implement this multiple ways.in here I use the Tag
Concept , for example
Step-1
in your ViewDidLoad
, set as your tableview.tag=1;
Step-2
- (IBAction)segBtnTapped:(id)sender {
if(yourSegmentControl.selectedSegmentIndex==0){
// email
tableview.tag=1;
}
else if(segControlForColor.selectedSegmentIndex==1){
// phone
tableview.tag=2;
}
else{
// titles
tableview.tag=3;
}
[yourtableView reloadData];
}
Step-3
no need to change on numberof rows in section or anything just call in your CellForRowatIndexpath
and didSelectrowatIndexpath
, for ex
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(tableview.tag == 1)
{
//code for email
cell.textLabel.text =[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"email"];
}
else if(tableview.tag == 2)
{
//code for phone
cell.textLabel.text =[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"phone"];
}
else if(tableview.tag == 3)
{
//code for titles
cell.textLabel.text =[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"titles"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableview.tag == 1)
{
//code for email
NSLog(@"email==%@",[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"email"]);
}
else if(tableview.tag == 2)
{
//code for phone
NSLog(@"phone==%@",[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"phone"]);
}
else if(tableview.tag == 3)
{
NSLog(@"title==%@",[[yourarrayName objectAtIndex:indexPath.row]objectForKey:@"titles"]);
}
}

Anbu.Karthik
- 82,064
- 23
- 174
- 143
-
Nice. But in the above code, I can display Phone numbers and emails directly in the tableview, but I want to show the contact names with emails once and contact names with phone numbers next time . – Naveen Mar 03 '16 at 13:18
-
-
-
@Naveen -- happy to hear bro. if my answer is useful give the upvote for the answer , it is useful in future – Anbu.Karthik Mar 04 '16 at 06:47
-
Hi, I am having another issue. I want to get my navigation controller title in this format Invite(3/50), 3 is number of tableview rows I selected and 50 is constant. Please help – Naveen Mar 04 '16 at 09:53
-
simple do like self.navigationController.navigationBar.topItem.title = [NSString stringWithFormat:@"Invite(%d/50)",yourarrayname.count]; – Anbu.Karthik Mar 04 '16 at 09:59
-
Hi, When I select the each row in tableview the arraycount should increase by 1. but currently for mutliple rows I am selecting the count is always 1. – Naveen Mar 04 '16 at 10:29
-
-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FFContactInviteCell *contactCell = (FFContactInviteCell *) [self.contactsTableview cellForRowAtIndexPath:indexPath]; contactCell.contactNameLabel.textColor = [UIColor tabBarTitleBlueColor]; self.selectedContactsList = [self.contactsTableview indexPathsForSelectedRows]; self.title = [NSString stringWithFormat:@"Invite(%lu/50)",(unsigned long)self.selectedContactsList.count]; } – Naveen Mar 04 '16 at 10:31
-
My navigation bar title (0/50), numerator should increase everytime I select a row in a tableview – Naveen Mar 04 '16 at 10:31
-
are you allocate the memory for this `self.selectedContactsList ` at the same time you were assigned to array , that the reason you get only one value, – Anbu.Karthik Mar 04 '16 at 10:33
-
-
-
When I am tapping on the cell it is selecting multiple times, like I am tapping on first cell 7 times, count is increased to 7. But it should be limited to 1 for each cell. – Naveen Mar 04 '16 at 10:41
-
then you need to create the one , see this http://stackoverflow.com/questions/23727255/multiple-checkmark-when-row-selected-in-uitableview-ios – Anbu.Karthik Mar 04 '16 at 10:44
0
It can be done by just checking the segmentedControl index state in the below delegate method.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if segmentedControl.selectedSegmentIndex == 0 {
// Load phone values from dictionary
....
}
else {
// Load email values from dictionary
....
}
}
Don't forget to reload table when segmented control index gets changed.

AnonymousRacer
- 141
- 6
-
I can do this, but my dictionary is having values for keys "name", "email", "phone". In one case I want to show contacts having email and in next i need to show contacts with phone numbers. How can I separate them. – Naveen Mar 03 '16 at 12:58
-
Traverse through the array of dictionary and use value of "phone" key for selectedsegmentindex 0 – AnonymousRacer Mar 03 '16 at 13:01