uitableview重新加载数据缓存?

发布于 2024-12-28 11:39:28 字数 5768 浏览 1 评论 0原文

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

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

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

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

所以,问题是表视图的前 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;
}

I'm having some problems with an UITableView and the method reloadData.

I'm refreshing the tableView when clicking over a refresh button with the last info in a data base ordered descendingly so the most recent items should appear in first position.

Every cell in my tableview is being filled with custom labels and uiimageviews.

Well... the thing is that when I press that refresh button and new data is found, the first 5 rows (which are the ones that are being shown in the display) aren't updated but the following rows do. However, if I press over any of the 5 first rows, they call correctly to the tableView:didSelectRowAtIndexPath: method with the new data.

So, the problem is that the tableview's "visual" content of the first 5 rows is not being updated.

Anyone can help me with this?

Thanks!

Code:

- (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2025-01-04 11:39:28

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

当您重新加载表时,此调用 UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
将为您提供一个可重复使用的单元格(读取已包含内容)。

但您的代码仅适用于“新鲜”单元格: if (cell == nil) {
,你只是还没有准备好应对这种情况......

Hmm, so you give each cell a unique cell identifier. Do you have a reason for that?
Mostly people try to patch their misunderstanding of iOS table views that way ...

When you reload your table this call UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
will get you a cell to reuse (read already contains content).

But your code only works for "fresh" cells : if (cell == nil) {
, you're just not prepared for that case ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文