【问题标题】:UITableViewCell changes its label value every time I scrollUITableViewCell 每次滚动时都会更改其标签值
【发布时间】:2020-02-24 15:43:01
【问题描述】:

我有一个 reusableCell,它有一个标签,其值由数组提供。但是标签和约束的值每次都会改变,我的 cellForRowAtIndexPath 代码看起来像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     if(indexPath.row % 2 == 0)
    {
        isReceived =TRUE;
    }
     else{
        isReceived = FALSE;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];

    ChatMessageCellTableViewCell *messageCell = (ChatMessageCellTableViewCell*) cell;
    if(messageCell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatMessageCellTableViewCell" owner:self options:nil];

        messageCell = [nib objectAtIndex:0];
    }
    else{
        [[messageCell formLabel]setText:messages[indexPath.row]];
        [messageCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    } // this else part is just to make sure that the if has a else part as I saw many people it will work , but in my case it didn't help.

    [[self tableView] setEstimatedRowHeight:50.0];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];

    return messageCell;
}

当我滚动表格时,值会发生变化。我尝试了许多不同的方法,但找不到解决方案,我很困惑,不得不发布我的问题。

谢谢

【问题讨论】:

  • 你把笔尖注册到 UITableView 了吗?例如[self.tableView registerNib:[UINib nibWithNibName:@"ChatMessageCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"id"];
  • 是的,我认为这是我的问题,因为我没有向 UITableView 注册任何笔尖。

标签: ios objective-c iphone uitableview


【解决方案1】:

我知道回答你自己的帖子是不好的做法,但我希望这对某人有所帮助

我能够通过更改方法 cellForRowAtIndexPath 来避免这些更改

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

    NSString *cellIdentifier = (indexPath.row % 2 == 0 ? @"EvenCell" : @"OddCell"); //using different identifier for every cell did the trick I guess
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    ChatMessageCellTableViewCell *messageCell = (ChatMessageCellTableViewCell*) cell; ;
    if (messageCell == nil) {
        messageCell = [[ChatMessageCellTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
            reuseIdentifier:cellIdentifier];
        if(indexPath.row % 2 == 0)
        {
            isReceived =TRUE;
        }
        else{
            isReceived = FALSE;
        }
    }

        [[messageCell formLabel]setText:messages[indexPath.row]];
        [messageCell setSelectionStyle:UITableViewCellSelectionStyleNone];

    [[self tableView] setEstimatedRowHeight:50.0];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];

    return messageCell;
}

我已经评论了我所做的更改,以防止在滚动时更改单元格。

感谢大家的支持:)

【讨论】:

  • 您最初的问题不完整 - 没有提及您使用的多种类型的单元格,因此为什么人们回答的内容对您不起作用。下次请详细一点。
  • 很抱歉,下次会确保的。
【解决方案2】:

我不熟悉 Obj-C,但这可能对你有帮助。

if youHaveTitleText {
cell.title.text = data.displayText
} else {
cell.title.text = ""
}

【讨论】:

  • 您的意思是仅在文本可用时设置标签文本,如果不设置为空?
  • 文本是从字符串数组中获取的,所以很明显可以设置字符串@SureshMopidevi。
【解决方案3】:

您的代码中存在错误的逻辑,您的代码仅为重复使用的单元格分配数据,而不是为新创建的单元格分配数据。您应该像这样将 else 括号中的代码移出。

ChatMessageCellTableViewCell *messageCell = (ChatMessageCellTableViewCell*) cell;
if(messageCell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatMessageCellTableViewCell" owner:self options:nil];

    messageCell = [nib objectAtIndex:0];
}
[[messageCell formLabel]setText:messages[indexPath.row]];
[messageCell setSelectionStyle:UITableViewCellSelectionStyleNone];

【讨论】:

  • 无论你建议什么我都试过了,但没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
相关资源
最近更新 更多