0

テーブルビュー内に2つのセクションがあり、1つの配列から1つのセクションをロードし、別の配列から1つのセクションをロードしました。両方のセクションのセル値を設定するにはどうすればよいですか?

私は次のコードを使用しています:

ヘッダーセクションのタイトル。

   - (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メソッドでも、セクションをチェックし、それに応じてデータをロードするための条件を1つ設定します。

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、それはテーブルビューがどのセクションに移入したいかを教えてくれます。

于 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 に答える