Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 每次滚动时,UITableViewCell都会更改其标签值_Ios_Objective C_Iphone_Uitableview - Fatal编程技术网

Ios 每次滚动时,UITableViewCell都会更改其标签值

Ios 每次滚动时,UITableViewCell都会更改其标签值,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有一个可重用单元,它有一个标签,数组为其提供值。但是标签和约束的值每次都会发生变化,我的CellForRowatinedexPath代码如下所示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row % 2 == 0) { isReceived =TRUE; }

我有一个可重用单元,它有一个标签,数组为其提供值。但是标签和约束的值每次都会发生变化,我的CellForRowatinedexPath代码如下所示

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

当我滚动表格时,值正在更改。我尝试了许多不同的方法,但找不到解决办法,我太困惑了,不得不把我的问题贴出来


谢谢

您的代码中有一个错误的逻辑,您的代码只是为重用的单元分配数据,而不是为新创建的单元分配数据。您应该像这样将代码移入或移出括号

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];
我不熟悉Obj-C,但这可能对您有所帮助

如果您有titletext{
cell.title.text=data.displayText
}否则{
cell.title.text=“”
}

我知道回答自己的帖子是不好的做法,但我希望这能帮助别人

通过更改CellForRowatineXpath方法,我可以避免这些更改

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

我已经对我所做的更改进行了注释,以防止滚动时单元格中的更改


感谢您的支持:)

无论您建议什么,我都尝试过了,但没有用。您的意思是只在文本可用时设置标签文本,如果没有设置为空?文本取自字符串数组,因此显然可以设置字符串@SureshMopidevi。您是否将nib注册到UITableView?e、 g.[self.tableView注册表项nb:[UINib nibWithNibName:@“ChatMessageCellTableViewCell”捆绑包:nil]forCellReuseIdentifier:@“id”];是的,我认为这是我的问题,因为我没有向UITableView注册任何nib。您最初的问题不完整-没有提到您使用的多种类型的单元格,因此人们回答的问题对您不起作用。请下次再详细一点。对不起,我们下次会确保的。