uitableview reloaddata cache?

时间:2012-01-22 16:02:34

标签: ios uitableview reloaddata

我在使用UITableView和reloadData方法时遇到了一些问题。

当点击刷新按钮时,我正在刷新tableView,数据库中的最后一个信息按递减顺序排列,因此最近的项目应该出现在第一个位置。

我的tableview中的每个单元格都充满了自定义标签和uiimageviews。

嗯......问题是,当我按下刷新按钮并找到新数据时,前5行(显示中显示的那些)不会更新,但以下行会更新。但是,如果我按下第5行中的任何一行,它们就会使用新数据正确调用tableView:didSelectRowAtIndexPath:方法。

所以,问题是tableview的前5行的“可视”内容没有更新。

任何人都可以帮我这个吗?

谢谢!

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [ NSString stringWithFormat: @"%d:%d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition:1 ]];

    UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease];

        // Configure the cell...
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        Activity *activity = [[self.activities valueForKey:[[[self.activities allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        cell.textLabel.text=@"";
        //Mostramos el nombre
        UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(60, -5, 200, 34)];
        label.text = [activity name];
        label.font = [UIFont boldSystemFontOfSize:17];
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        [label release];

        //Mostramos la imagen
        UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 3, 50, 50)];
        [cell addSubview:leftImage];


        UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(60, 25, 200, 38)];
        textView.editable = NO;
        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor grayColor];
        textView.font = [UIFont systemFontOfSize:12.0];
        textView.contentInset = UIEdgeInsetsMake(-8,-8,0,0);
        textView.userInteractionEnabled=NO;
        [cell addSubview:textView];

        switch ([activity notType]) {
            case 0:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"requestPUSH",@""),[activity name]];
                leftImage.image = [UIImage imageNamed:@"ios.png"];
                break;
            case 1:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"MPCreatedPushString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"mpNew.png"];
                break;
            case 2:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"MPUpdatedPushString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"mpUpdated.png"];
                break;
            case 3:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"MPDeletedPushString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"ios.png"];
                break;
            case 4:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"MPGuestConfirmationPUSHString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"attend.png"];
                break;
            case 5:
                if ([[activity message] isEqualToString:@"noData"]) {
                    textView.text = [NSString stringWithFormat:NSLocalizedString(@"ShoutPushString",@""),[activity name]];
                }else{
                    textView.text = [NSString stringWithFormat:NSLocalizedString(@"ShoutPushStringWithData",@""),[activity name], [activity message]];
                }
                UIImage *contactImage = [UIImage imageWithData:[[activity person] pic]];
                if (contactImage!=nil) {
                    leftImage.image = contactImage;
                    //redondeamos bordes
                    CALayer * l = [leftImage layer];
                    [l setMasksToBounds:YES];
                    [l setCornerRadius:5.0];
                }else{
                    leftImage.image = [UIImage imageNamed:@"ios.png"];
                }
                break;
            case 6:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"CheckinPushString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"ios.png"];
                break;
            case 7:
                textView.text = [NSString stringWithFormat:NSLocalizedString(@"MPGuestRejectionPUSHString",@""),[activity name],[activity nameItem]];
                leftImage.image = [UIImage imageNamed:@"reject.png"];
                break;
            default:
                break;
        }
        [leftImage release];
        [textView release];


        //Mostrar fecha
        double timestamp = [[activity datetime] doubleValue]/1000;
        NSString *dateShow;

        NSDate *datetime = [NSDate dateWithTimeIntervalSince1970:timestamp];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"HH:mm"];
        dateShow=[dateFormat stringFromDate:datetime];

        UILabel *label2= [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 20)];
        label2.text = dateShow;
        label2.textAlignment = UITextAlignmentRight;
        label2.font = [UIFont systemFontOfSize:12];
        label2.backgroundColor = [UIColor clearColor];
        label2.textColor = [UIColor blueColor];
        [cell addSubview:label2];
        [label2 release];

    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

嗯,所以你给每个小区一个唯一的小区标识符。你有理由吗? 大多数人试图通过这种方式修补他们对iOS表格视图的误解......

当您重新加载表格时,此调用UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 会让您重复使用单元格(已读取已包含内容)。

但是您的代码仅适用于“新鲜”单元格:if (cell == nil) { ,您只是没有为此案做好准备......