【问题标题】:uitableview cells display wrong data when off screen at loading time加载时离开屏幕时,uitableview 单元格显示错误数据
【发布时间】:2014-05-13 07:16:21
【问题描述】:

我有一个超出屏幕区域的UITableView。我显示了来自NSMutableArray 的一些数据,其中填充了NSDictionary 对象。所有对象都具有group 属性,即truefalse,如果group 为真,则应在单元格中显示UIButton(以展开组)。
我在storyboard 的单元格prototype 中创建了一个UIButton。我给了它alpha 0。如果我加载列表,则按钮不在任何单元格中(duh)。这是我的cellForRowAtIndexPath

if ([[contacts[indexPath.section] valueForKey:@"group"] isEqualToString:@"true"]) {
    UIButton *button = (UIButton*)[cell viewWithTag:999];
    [button setAlpha:1.0f];
}

如果group 属性为true,则使按钮可见。在我当前的测试用例中,前三个对象的group 设置为true,其余的group 设置为false。现在我的问题是,前三个单元格有一个可见按钮(如预期的那样),但表格中更远的一些单元格也有一个。我在上面的if statement中放了一个断点,它真的只进去了3次。

它以一种模式发生,前三个单元格有一个按钮,然后五个单元格没有,然后另外三个单元格有一个可见按钮。比另外五个没有按钮,最后一个单元格有(我想如果我添加两个单元格,它们也会有一个按钮)

第二组有按钮的单元格就在屏幕外开始,所以这可能与它有关。

更相关的事情,还有一些显示的文本是我从与组标志相同的数组中获取的,该文本显示正确。所以并不是细胞以某种方式被复制,只是if statement 部分搞砸了。

我仔细检查了程序进入if statement 的次数,实际上只有三次(前三个条目)。如果我评论按钮上的setAlpha 调用,任何地方都不会出现按钮,因此也没有其他地方可以看到按钮。

我在装有 iOS 7.1 的 iPad 4 上进行测试。

如果我降低行的高度以使所有行都适合屏幕,那么问题就消失了,所以这肯定与加载数据时行落在屏幕区域之外有关。


编辑

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    UIColor *color = [UIColor colorWithRed:(1.0f/255.0f)*220 green:(1.0f/255.0f)*220 blue:(1.0f/255.0f)*225 alpha:1.0f];

    if (indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"single" forIndexPath:indexPath];

        UILabel *screenname = (UILabel*)[cell viewWithTag:10];
        UILabel *username = (UILabel*)[cell viewWithTag:11];
        UIImageView *avatar = (UIImageView*)[cell viewWithTag:20];

        if ([[contacts[indexPath.section] valueForKey:@"group"] isEqualToString:@"true"]) {
            NSDictionary *c = contacts[indexPath.section];

            UIButton *button = (UIButton*)[cell viewWithTag:999];
            [button setAlpha:1.0f];

            button.tag = indexPath.section;
            [button addTarget:self action:@selector(onGroupButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            int numUsersInGroup = [[c valueForKey:@"users"] count];
            NSString *numUsersInGroupString = [NSString stringWithFormat:@"%d",numUsersInGroup];
            [button setTitle:numUsersInGroupString forState:0];
            [button setNeedsDisplay];

            screenname.text = @"Groep";
            username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"groupName"]];
        } else {
            User *c = (User*)contacts[indexPath.section];

            UIButton *button = (UIButton*)[cell viewWithTag:999];
            [button setAlpha:0.0f];

            screenname.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"screenName"]];
            username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"username"]];

            [avatar setImage:[UIImage imageNamed:@"avatar.png"]];
        }

        [GeneralFunctions setFontFamily:@"KozGoPro-ExtraLight" forView:cell andSubViews:YES];
        [GeneralFunctions setFontFamily:@"KozGoPro-Medium" forView:screenname andSubViews:YES];

        [cell.layer setShadowColor:[color CGColor]];
        [cell.layer setShadowOffset:CGSizeMake(0,4)];
        [cell.layer setShadowRadius:3.0f];
        [cell.layer setShadowOpacity:1.0f];
        cell.clipsToBounds = NO;
        cell.layer.masksToBounds = NO;
    }

    if (indexPath.row > 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"group" forIndexPath:indexPath];

        UILabel *screenname = (UILabel*)[cell viewWithTag:10];
        UILabel *username = (UILabel*)[cell viewWithTag:11];

        User *c = (User*)[contacts[indexPath.section] valueForKey:@"users"][indexPath.row-1];

        screenname.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"screenName"]];
        username.text = [NSString stringWithFormat:@"%@",[c valueForKey:@"username"]];

        [GeneralFunctions setFontFamily:@"KozGoPro-ExtraLight" forView:cell andSubViews:YES];
        [GeneralFunctions setFontFamily:@"KozGoPro-Medium" forView:screenname andSubViews:YES];
    }

    return cell;
}

【问题讨论】:

    标签: ios iphone objective-c ipad uitableview


    【解决方案1】:

    重复使用您的单元格时,您不会将按钮设置回0.0 alpha。每次重复使用单元格时都需要重置单元格或更改单元格的属性,否则它将显示旧内容

    if ([[contacts[indexPath.section] valueForKey:@"group"] isEqualToString:@"true"]) {
        NSDictionary *c = contacts[indexPath.section];
    
        UIButton *button = (UIButton*)[cell viewWithTag:999];
        [button setAlpha:1.0f];
    
        //this line changes your tag and hence in else condition for reused cell it'll come nil
        button.tag = indexPath.section;
    
    } else {
        UIButton *button = (UIButton*)[cell viewWithTag:999];
        [button setAlpha:0.0f];
    }
    

    建议不要按标签访问按钮,而是通过使用属性按类型将单元格转换为自定义单元格来访问它

    【讨论】:

    • 遗憾的是,这并不能解决问题。如何“重置我的手机”?
    • cellForRowAtIndexPath code。很乱,抱歉。
    • 取消注释此代码button.tag = indexPath.section;。在这里,您正在更改按钮的标签,当您尝试通过标签 999 获取视图时,您将得到 nil。
    • 我需要设置按钮的标签(我在“获取”按钮后这样做)才能知道在我的按钮回调中按下了哪个按钮。我现在会尝试评论它。
    • 好的,评论那行有效,谢谢:)。现在我只需要找到另一种方法来检测按下了哪个按钮.....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多