2012-04-06 70 views
0

我有一個表格視圖中的2個部分,我已經從一個數組加載一個部分,另一個從另一個。我如何設置兩個部分的單元格值?如何在表格視圖的兩部分加載數據iPhone

我使用的是以下代碼:

標題部分的標題。

- (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; 


     } 
    } 
+0

什麼是你的問題? – EightyEight 2012-04-06 05:03:34

+0

問題如何在單元格表視圖中爲兩個部分加載值 – 2012-04-06 05:04:54

+0

+1,因爲在提問之前已經做了一些努力。 – 2012-04-06 05:06:16

回答

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]; 

} 
+0

,但它給出了異常,而不是運行 – 2012-04-06 05:16:10

+0

原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空數組的界限'當它出現第二個數組行時 – 2012-04-06 05:17:43

+1

它是因爲你的數組是空的而你試圖獲取數據從中。它就像是試圖從一臺沒有錢的ATM機上提取1000盧比。 – 2012-04-06 05:21:29

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 
0

documentationUITableViewDataSourceUITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property),

所以你可以去indexPath.section,它會告訴你tableview要填充哪一部分。

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]; 

} 
相關問題