0

我在表格视图中有 2 个部分,我从一个数组加载了一个部分,从另一个加载了一个部分。如何设置两个部分的单元格值?

我正在使用以下代码:

标题部分的标题。

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:                             (NSInteger)section
   {
        if(section == 0)       
            return @" Scheduled Patients ";
        else
            return @" Walk In Patients ";
   }

部分中的行:

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

    if(section==0)
        return [resultArray count];
    else {
      EMRAppDelegate *appDelegate = (EMRAppDelegate *)[[UIApplication sharedApplication] delegate];
      return [appDelegate.walkPatients count];
    }
  }


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


     {
       UIButton * uploadButton = nil;
       static NSString * Identifier = @"Identifier";
       UITableViewCell * cell = [table dequeueReusableCellWithIdentifier:Identifier];
      if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier] autorelease];
       uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
       [uploadButton setFrame:CGRectMake(10, 10, 24, 24)];
       [uploadButton setImage:[UIImage imageNamed:@"upload.png"] forState:UIControlStateNormal];
       [uploadButton addTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
       [uploadButton setTag:999];
       cell.accessoryView = uploadButton;
      }
     else {
     uploadButton = (UIButton *)cell.accessoryView;
     }



     EMRAppDelegate *appDelegate = (EMRAppDelegate *)[[UIApplication sharedApplication] delegate];


   ObjectData *theCellData =[resultArray objectAtIndex:indexPath.row];

   WalkPatient * patient = [ appDelegate.walkPatients objectAtIndex:indexPath.row];

   if (indexPath.section == 0)
   {
    // do coding for cell loading for Scheduled Patients 


     cell.textLabel.font = [UIFont systemFontOfSize:18];
     cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", theCellData.firstName, theCellData.lasttName];

      if (patient.syncDate != nil) {
        UIImageView * checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
        [checkmark setFrame:CGRectMake(12, 0, 12, 12)];
        [checkmark setTag:998];
        [uploadButton addSubview:checkmark];
        [checkmark release];
        [uploadButton removeTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Synched: %@", [dateFormatter stringFromDate:patient.syncDate]];
        [dateFormatter release];
         } 
        else {
        UIView * checkmark = (UIView *)[uploadButton viewWithTag:998];
        if (checkmark != nil) {
            [checkmark removeFromSuperview];
         }
       }

       cell.selectionStyle = UITableViewCellSelectionStyleNone;

      return cell;

       }

    else
     {
    // do load cells for    Walk In Patients. 





       cell.textLabel.font = [UIFont systemFontOfSize:18];
       cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", patient.firstName, patient.lastName];

       if (patient.syncDate != nil) {
       UIImageView * checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
       [checkmark setFrame:CGRectMake(12, 0, 12, 12)];
       [checkmark setTag:998];
       [uploadButton addSubview:checkmark];
       [checkmark release];
       [uploadButton removeTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
         cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
         cell.detailTextLabel.text = [NSString stringWithFormat:@"Synched: %@", [dateFormatter stringFromDate:patient.syncDate]];
         [dateFormatter release];
      } 
      else {
        UIView * checkmark = (UIView *)[uploadButton viewWithTag:998];
        if (checkmark != nil) {
        [checkmark removeFromSuperview];
        }
      }

      cell.selectionStyle = UITableViewCellSelectionStyleNone;

       return cell;


      }
   }
4

4 回答 4

1

这很简单。在cellForRowAtIndexPath方法中,您还设置了一个条件来检查部分并根据它们加载数据。

if (indexPath.section == 0)
{
    // do coding for cell loading for Scheduled Patients 
      UIButton *btnsection0=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];

}

else
{
     // do load cells for    Walk In Patients. 
      UIButton *btnsection1=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];

 }
于 2012-04-06T05:05:33.887 回答
0

你几乎明白了。cellForRowAtIndexPath 方法带有一个 indexPath 参数,可以为您提供:

NSInteger row = indexPath.row;
NSInteger section = indexPath.section;

请始终使 cellForRowAtIndexPath 逻辑镜像 numberOfRowsInSection 逻辑,因此当您被要求提供行数时:

return (section==0)? [array0 count] : [array1 count];

...当您被要求提供一个单元格时,您需要该单元格的数据

NSInteger row = indexPath.row;
NSInteger section = indexPath.section;

NSArray *arrayForThisSection = (section==0)? array0 : array1;
id elementForThisRow = [arrayForThisSection objectAtIndex:row];

// config the cell with elementForThisRow
于 2012-04-06T05:06:49.207 回答
0

UITableViewDataSource的文档中: UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property),

所以你可以去indexPath.section,它会告诉你 tableview 想要填充哪个部分。

于 2012-04-06T05:08:33.597 回答
0
UIButton *btnsection0=[UIButton buttonWithType:UIButtonTypeCustom];
if (indexPath.section == 0)
{ 
  btnsection0.tag =  [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
}else{ 
  btnsection0.tag = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
}
[btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];

按钮事件

-(IBAction)showProject:(id)sender{
      NSString *s = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
      int section = [[s substringToIndex:1] intValue];
      int row = [[s substringFromIndex:0] intValue];

}
于 2012-04-06T12:16:08.500 回答