1
votes

here is my code, in my app, I use a custom UITableViewCell for each cell in the UITableView, and calculate the cell height in "heightForRowAtIndexPath", but if I use "dequeueReusableCellWithIdentifier" , the cell is overlapped when scroll the table view. and issue gone when don't use "dequeueReusableCellWithIdentifier". don't know why this issue happen?

actually, I want to know if not use "dequeueReusableCellWithIdentifier" to create the cell, does there will cause any memory issue if the tableview display a lot cells?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.

    return 10;

}

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


    static NSString* cellIdentifier = @"threadCell";
    SYGBBSTableViewCell * cell=nil;

    //if I comment below line code , the cell overlap issue solved 
    cell = (SYGBBSTableViewCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[SYGBBSTableViewCell alloc] initMessagingCellWithReuseIdentifier:cellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath];





    return cell;
}



-(void)configureCell:(SYGBBSTableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath {

    SYGBBSTableViewCell* ccell = (SYGBBSTableViewCell*)cell;

    ...
    cell.content.text=content;

}








- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
    /// Here you can set also height according to your section and row
    NSDictionary* thread = [MyGoController getBBSThreadAtIndex:indexPath.row];

    int height = [SYGBBSTableViewCell cellHeightForThreadAt:thread];
    return height;
}
2
You don't need to check if (cell == nil) - meda
@meda I think he does this so he can comment in/out the dequeue line to test. - danh
@sureone, is the class of the prototype cell set in IB to SYGBBSTableViewCell? If not, that would explain the problem. - danh
It is not in IB, and there are two UILabels in the cell, and I added programmatically in "initMessagingCellWithReuseIdentifier" of the UITableViewCell subclass. - sureone
and actually, I want to know if not use "dequeueReusableCellWithIdentifier" to create the cell, does there will cause any memory issue if the tableview display a lot cells? - sureone

2 Answers

0
votes

The issue is that when you get a cell using dequeueReusableCellWithIdentifier it will not call your initializer initMessagingCellWithReuseIdentifier. Rename initMessagingCellWithReuseIdentifier to initWithStyle:reuseIdentifier: and it should work.

Notice that initWithStyle:reuseIdentifier: is the designated initializer (Doc: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html) and you should call [super initWithStyle:style reuseIdentifier:reuseIdentifier];. After that, implement the logic you are now implementing in initMessagingCellWithReuseIdentifier.

0
votes

dequeueReusableCellWithIdentifier is useful for cell reuse, and you should check cell == nil, unless you use - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath to get reusable cell. I think the problem is configureCell, because cells height change, you should configure cells with proper height as well, you can try code like this in configureCell method:

-(void)configureCell:(SYGBBSTableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath {

    SYGBBSTableViewCell* cell = (SYGBBSTableViewCell*)cell;
    CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];

    [cell layoutSubviewWithHeight:cellHeight];
    [cell configureContent];
}