【问题标题】:Ceating UITableViewCell on each time cellForRowAtIndexPath calls每次调用 cellForRowAtIndexPath 时作弊 UITableViewCell
【发布时间】:2010-11-06 06:28:25
【问题描述】:

我创建了一个自定义 UITableView 单元格,它有 4-5 个不同的组件。有时我从 CGRectMake 创建 UIViews,有时基于一些 x、y 坐标计算我定位一些 UIImages。一切正常,因为 cellForRowAtIndexPath 每次创建一个新单元格。但是,当我要基于“dequeueReusableCellWithIdentifier”重用单元格时,UI 会变得一团糟。以前的 rect 制作永远不会被删除,并且早期 UIViews 的定位也表现得很奇怪。尽管单元格索引是变化的,但它并不反映绘制的 UIImages 的协调性。

在每个 cellForRowAtIndexPath 中创建 UITableViewCell 在 3GS 和 4G 中都可以正常工作,但在 3G 中确实很烦人并且速度非常慢。我怀疑这种行为是因为在每次调用中都创建了这个 cellForRowAtIndexPath。那么任何人都可以帮助我确定这个问题。

这就是我现在所做的。

CustomNoteCell *cell = nil;

if (cell == nil) { 

    cell = (CustomNoteCell *)[tableView dequeueReusableCellWithIdentifier: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;

谢谢

【问题讨论】:

    标签: iphone iphone-sdk-3.0 uitableview ios4


    【解决方案1】:

    我认为您将 dequeue 单元调用放置在错误的位置。你应该把它放在if (cell == nil)之前。

    【讨论】:

    • 是的。我已经发布了工作示例。在这种情况下,它工作正常。但在 3G 中速度较慢,但​​在 3GS 和 4G 中完美运行。但是当我在 if (cell == nil) 之前将单元格出列时,我得到了我讨论的问题。所以这是我的问题。我想在该行之上出列,它应该按预期工作,但目前不是。
    • 你能发布你的实际代码,这些代码不起作用,并带有格式吗?我现在眼睛疼……
    • 很抱歉。格式化了。
    【解决方案2】:

    正如 tia 所提到的,您的单元格总是为零。你应该尝试早点出队。

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

    ...

    【讨论】:

    • 谢谢乔什保罗。当我要基于“dequeueReusableCellWithIdentifier”重用单元格时,UI 会变得混乱。这就是问题所在。
    • 什么是“搞砸了”?这种方法会发生什么?静态 NSString *CellIdentifier = @"CustomNoteCell"; CustomNoteCell *cell = (CustomNoteCell *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomNoteCell" owner:self options:nil] lastObject]; } // 自定义单元格...
    • 这里的问题是定位出了问题。它在每个单元格中随机绘制内容。
    • 如何设置单元格的高度?您可以将其中的一些布局逻辑推送到您创建的 CustomNoteCell 类中吗?即 [cell setNoteText: noteValue]; ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多