IPhone Storyboard custom cell disappears when scro

2019-08-11 01:18发布

I have just finished a project where I use a custom cell for my TableView in Storyboard.

The problem is that when i scroll down the content in each cell is gone, which in my case is two Labels.

This is the code I use to present each cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    MessageCell *cell = (MessageCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Message *messageForRow = (Message *)[messages objectAtIndex:[indexPath row]];
    [cell.messageLabel setText:[messageForRow message]];
    [cell.senderLabel setText:[messageForRow sender]];

    return cell;
}

I have specified the right cellidentifier in storyboard and linked the Class to my custom cell class.

What can be wrong? If there is any needed information I have missed to present, please tell me.

Best regard Robert

2条回答
劫难
2楼-- · 2019-08-11 01:38

Could you check by logging that this method is called when scrolling?

查看更多
一夜七次
3楼-- · 2019-08-11 01:44

I solved it by myself, the problem did not lie within the update method. By mistake I used the weak signature for the properties in my Message class.

@property (nonatomic, weak) NSString *sender;

I solved it by using the strong signature instead.

@property (nonatomic, strong) NSString *sender;

This was a great lesson because I didn't fully understand the concept of strong and weak.

查看更多
登录 后发表回答