Out of range object in UItableview data source object list
I bind the UITableview with NSMutableArray DataSource.I want to get selected NSMutableArray object when user select on tableview.I got the object but I can't access object properties and application quite if I access object's properties. I try it on didSelectRowAtIndexPath method.Here is my coding.
-(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]
There are multiple things you have to check if an object is out of bounds in a uitableview.
1: Check if the numbersOfRowsInSection is done properly.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.myArray.count;
}
Since you haven't posted this part I assume you set this the right way.
2: You are removing objects without updating the table properly so the table is showing old data.
[self.myTableView reloadData];
This should update the UITableview data to the most recent data, and it should be done when you change your datasource array.
Try this one : DoctorItem *physician= (DoctorItem *)[[tableDataList objectAtIndex:indexPath.row] copy];
and access property with : [physician DName]
精彩评论