开发者

Ceating UITableViewCell on each time cellForRowAtIndexPath calls

开发者 https://www.devze.com 2023-01-23 14:27 出处:网络
I have created a custom UITableView cell and it has 4-5 various components. Some times I create UIViews from CGRectMake and some times based on some x, y coordinate calculations I position some UIImag

I have created a custom UITableView cell and it has 4-5 various components. Some times I create UIViews from CGRectMake and some times based on some x, y coordinate calculations I position some UIImages. Everthing works fine cos cellForRowAtIndexPath every time creates a new cell. But when Im going to reuse the cell based on "dequeueReusableCellWithIdentifier" the UI gets messed up. The previous rect makes were never get erased and positioning of the earlier UIViews are also behaving weird. Although the cell indexes are changes it does not reflect the coordination of the drawn UIImages.

Creating the UITableViewCell in each cellForRowAtIndexPath works fine in 3GS and 4G but its really annoying and its very slow in 3G. I suspect this behavior is because of creation of this cellForRowAtIndexPath in every call. So can any one help me to identify this problem.

This is what I do now.

CustomNoteCell *cell = nil;

if (cell == nil) { 

    cell = (CustomNoteCell *)[tableView dequeueReusableCellWithIdentifi开发者_Go百科er:CellIdentifier]; 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomNoteCell" owner:nil options:nil];


for (id currentObject in topLevelObjects) { 

    if ([currentObject isKindOfClass:[UITableViewCell class]]) { 

             cell = (CustomNoteCell *) currentObject;  
             break; 
     } 
} 
}

//rest of the codes go here like 
CGSize maximumLablelSize = CGSizeMake(296, 1000); 
CGSize expectedLabelSize = [alue sizeWithFont:cell.customCellNoteText.font constrainedToSize:maximumLablelSize lineBreakMode:cell.customCellNoteText.lineBreakMode]; 
CGRect newFrame = cell.customCellNoteText.frame; 
newFrame.size.height = expectedLabelSize.height; 
cell.customCellNoteText.frame = newFrame; 
cell.customCellNoteText.text = noteValue; 
cell.uiViewContrlloer = self; 
CGRect addressRect = cell.address.frame; 
addressRect.origin.y = newFrame.origin.y + newFrame.size.height + 10; 
cell.address.frame = addressRect;

Thank you


I think you place your dequeue cell call at the wrong place. You should put it before if (cell == nil).


As tia mentioned, your cell is always going to be nil. You should try to dequeue earlier.

CustomNoteCell *cell = (CustomNoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (! cell) {
    cell = [[[CustomNoteCell alloc] initWithReuseIdentifier:CellIdentifier] autorelease];

...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号