【问题标题】:Out of range object in UItableview data source object listUItableview 数据源对象列表中的对象超出范围
【发布时间】:2011-10-14 07:10:56
【问题描述】:

我将 UITableview 与 NSMutableArray DataSource 绑定。当用户在 tableview 上选择时,我想获取选定的 NSMutableArray 对象。我得到了对象,但如果我访问对象的属性,我就无法完全访问对象属性和应用程序。我尝试使用 didSelectRowAtIndexPath 方法。这是我的编码。

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

【问题讨论】:

标签: iphone objective-c ios cocoa-touch


【解决方案1】:

如果某个对象在 uitableview 中超出范围,您必须检查多项内容。

1:检查 numbersOfRowsInSection 是否正确完成。

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

由于您尚未发布此部分,我假设您设置正确。

2:您在没有正确更新表格的情况下删除对象,因此表格显示的是旧数据。

[self.myTableView reloadData];

这应该将 UITableview 数据更新为最新数据,并且应该在您更改数据源数组时完成。

【讨论】:

  • 还是问题。当我访问对象的属性时,应用程序相当,
  • 好吧,如果它不是困扰您的数组,它只能是您尝试访问的对象的存在。它在您访问它的那一刻就存在吗?对象中的数据在您访问它的那一刻就存在,您是否正确设置了这些值?
  • 我在 didSelectRowAtIndexPath 方法中访问对象的属性。当我选择行时,应用程序相当。
【解决方案2】:
Try this one : DoctorItem *physician= (DoctorItem *)[[tableDataList objectAtIndex:indexPath.row] copy];
and access property with : [physician DName]

【讨论】:

  • 还是问题。当我访问对象的属性时,应用程序相当,
  • 在自定义对象中检查这些变量的属性,在didSelectRowAtIndexPath中访问。
猜你喜欢
  • 2011-11-23
  • 1970-01-01
  • 2021-07-30
  • 2015-01-26
  • 2014-09-27
  • 2012-04-14
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多