【问题标题】:weird behavior in tableview iphonetableview iphone中的奇怪行为
【发布时间】:2011-06-28 11:44:03
【问题描述】:

这有点奇怪。我的应用程序中有一个表格视图,它显示从数据库中获取的数据。如果我向下滚动并选择一个单元格,则文本会发生变化,并且前一个单元格中的一行文本将放置在当前选定单元格的文本上方。我知道这可能是由于重用标识符,但我不知道如何解决这个问题。这是我正在使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UILabel* labelCourse;
    UILabel* labelPlace;

    static NSString *kCellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    Course *course = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        course = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        course = [self.courses objectAtIndex:indexPath.row];
    }    


    CGRect labelCourseFrame = CGRectMake(10, 10, 290, 25);
    labelCourse = [[UILabel alloc] initWithFrame:labelCourseFrame];
    labelCourse.font = [UIFont systemFontOfSize:14.0];
    labelCourse.font = [UIFont boldSystemFontOfSize:18];
    [cell.contentView addSubview:labelCourse];

    CGRect labelPlaceFrame = CGRectMake(10, 35, 290, 25);
    labelPlace = [[UILabel alloc] initWithFrame:labelPlaceFrame];
    labelPlace.font = [UIFont systemFontOfSize:12.0];
    labelPlace.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:labelPlace];

    labelCourse.text = course.name;
    labelPlace.text = course.location;

    [labelCourse release];        
    [labelPlace release];
//    cell.textLabel.textColor = [UIColor colorWithHexString:@""];

    return cell;
}

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案1】:

    您可以将创建部分移动到创建新单元格的if 子句内。

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        CGRect labelCourseFrame = CGRectMake(10, 10, 290, 25);
        labelCourse = [[UILabel alloc] initWithFrame:labelCourseFrame];
        labelCourse.font = [UIFont systemFontOfSize:14.0];
        labelCourse.font = [UIFont boldSystemFontOfSize:18];
        [cell.contentView addSubview:labelCourse];
    
        CGRect labelPlaceFrame = CGRectMake(10, 35, 290, 25);
        labelPlace = [[UILabel alloc] initWithFrame:labelPlaceFrame];
        labelPlace.font = [UIFont systemFontOfSize:12.0];
        labelPlace.textColor = [UIColor darkGrayColor];
        [cell.contentView addSubview:labelPlace];
    
        labelCourse.tag = 50;
        labelPlace.text = 51;
    
        [labelCourse release];        
        [labelPlace release];
    }
    

    如您所见,我已将tags 附加到视图,以便在重复使用单元格时检索标签。现在获取标签,

    UILabel * labelCourse = [cell viewWithTag:50];
    UILabel * labelPlace = [cell viewWithTag:51];
    

    并设置它们,

    labelCourse.text = course.name;
    labelPlace.text = course.location;    
    

    【讨论】:

    • 总之,标签起到了作用?谢谢,它现在正在工作。你拯救了我的一天。
    • 是的,对于这种情况。您还可以考虑创建一个自定义单元格,以便您可以直接使用属性(假设标签被声明为自定义单元格中的属性),而不必使用标签。
    • 这实际上并没有像我预期的那样工作。单元格显示错误的值。我不知道是不是因为我正在重用这些标签。我得到了 5 次相同的值,这意味着旧值正在替换新值。可能是什么原因?
    • @Ernesto 只是为了确认一下,您的cellForRowAtIndexPath: 方法是否与this pastie 中的方法相同。
    • 你可以投射它。 UILabel * labelCourse = (UILabel *)[cell viewWithTag:50];
    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多