2011-10-14 54 views
0

我將UITableview與NSMutableArray綁定DataSource.I想要在用戶選擇tableview.I時獲取選定的NSMutableArray對象獲取對象但我無法訪問對象屬性和應用程序我訪問對象的屬性。我在didSelectRowAtIndexPath方法上嘗試它。這裏是我的編碼。UItableview數據源對象列表中超出範圍的對象

-(UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    DoctorItem *physician=(DoctorItem*)[tableDataList objectAtIndex:indexPath.row]; 

    UILabel *lbName=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 290, 25)]; 
    [lbName setText:physician.DName]; 
    [cell.contentView addSubview:lbName]; 
    [lbName release]; 


    UILabel *lbQualifications=[[UILabel alloc]initWithFrame:CGRectMake(10,40,290,25)]; 
    [lbQualifications setText:physician.Qualifications]; 
    lbQualifications.textColor=[UIColor lightGrayColor]; 

    CGSize maximumLabelSize=CGSizeMake(296,9999); 
    CGSize expectedLabelSize=[physician.Qualifications sizeWithFont:lbQualifications.font 
                constrainedToSize:maximumLabelSize 
                 lineBreakMode:lbQualifications.lineBreakMode]; 

    CGRect newFrame=lbQualifications.frame; 
    newFrame.size.height=expectedLabelSize.height; 
    lbQualifications.frame=newFrame; 
    [cell.contentView addSubview:lbQualifications]; 
    lbQualifications.numberOfLines=0; 

    lbQualifications.lineBreakMode=UILineBreakModeWordWrap; 
    [lbQualifications release]; 
    UILabel *lbCategory=[[UILabel alloc]initWithFrame:CGRectMake(10,80,290,25)]; 
    [lbCategory setText:physician.CName]; 
    lbCategory.textColor=[UIColor lightGrayColor]; [cell.contentView addSubview:lbCategory]; 
    [lbCategory release]; 
    [physician release]; 

    return cell; 

} 

-(CGFloat)tableView :(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{ 
    return 110; 
} 

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
    DoctorItem *physician=[tableDataList objectAtIndex:indexPath.row]; 
    if(physician!=nil) 
    { 
     UIAlertView *alert=[[[UIAlertView alloc]initWithTitle:@"" message:physician.DName delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease]; 
     [alert addButtonWithTitle:@"OK"]; 
     [alert show]; 
    } 

    [physician release]; 
}                           It the following link solves my problem.[http://stackoverflow.com/questions/5761518/variable-is-not-a-cfstring-error][1] 
+0

了以下鏈接解決我的問題[http://stackoverflow.com/questions/5761518/variable-is-not-a-cfstring-error] [1] [1]:HTTP ://stackoverflow.com/questions/5761518/variable-is-not-a-cfstring-error –

回答

0

有很多事情你必須檢查一個對象是否超出了可用視圖的範圍。

1:檢查numbersOfRowsInSection是否正確完成。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.myArray.count; 
} 

既然您還未發佈此部分,我假設您設置了正確的方法。

2:您正在刪除對象而沒有正確更新表格,因此表格顯示的是舊數據。

[self.myTableView reloadData]; 

這應該將UITableview數據更新爲最近的數據,並且應該在更改數據源數組時更新它。

+0

仍然problem.When我訪問對象的屬性,應用程序相當, –

+0

好吧,如果它不是數組,它是可以竊聽你只是您嘗試訪問的對象的存在。它存在你訪問它的那一刻嗎?對象中的數據是否在您訪問它時存在,您是否正確設置了這些值? –

+0

我在didSelectRowAtIndexPath方法中訪問對象的屬性。當我選擇行時,應用程序相當。 –

0
Try this one : DoctorItem *physician= (DoctorItem *)[[tableDataList objectAtIndex:indexPath.row] copy]; 
and access property with : [physician DName] 
+0

仍然有問題。當我訪問對象的屬性時,應用程序相當, –

+0

檢查自定義對象中的變量屬性,這是在didSelectRowAtIndexPath中的訪問。 –