如何更改不同分段控制索引的数据源



我正在使用带有标题、电话和电子邮件的分段控件。我正在从地址簿中获取联系人详细信息并将其存储为字典数组。每个词典都带有键"姓名","电子邮件","图像","电话"。我的要求是在点击电子邮件时在表格视图中仅显示带有电子邮件的联系人,并在点击分段控件上的电话按钮时显示带有电话的联系人。请帮助我实现这一目标。

我们可以实现这个多重 ways.in 在这里我使用Tag概念,例如

步骤-1

在您的ViewDidLoad中,设置为您的tableview.tag=1;

步骤-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];
}

步骤-3

无需更改部分中的行数或任何内容,只需调用您的CellForRowatIndexpathdidSelectrowatIndexpath,例如

 -(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"]);
  }
}
只需

在下面的委托方法中检查 segmentedControl 索引状态即可完成。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if segmentedControl.selectedSegmentIndex == 0 {
        // Load phone values from dictionary 
        ....
     }
    else {
        // Load email values from dictionary
        ....
    }
}

不要忘记在分段控制索引更改时重新加载表。

最新更新