【问题标题】:How to NOT use dequeueReusableCellWithIdentifier?如何不使用 dequeueReusableCellWithIdentifier?
【发布时间】:2014-12-01 06:46:46
【问题描述】:

我有一个小表格视图,最多 10 个项目,我不想使用 dequeueReusableCellWithIdentifier 方法。原因是,每次滚动表格视图时,我都会用第一行代替最后一行,这需要几秒钟的时间来更新以获取最后一行的更新。

我正在使用原型单元,我尝试删除:

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

但它会在表格视图中返回空白单元格。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell=[[JsonTableViewCell alloc]initWithStyle:
          UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

我也尽量保持

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

并像这样配置单元格:

if (cell == nil) {
    cell.title.text = titleStrip;
}

但是在这种情况下,我得到了原型单元,就像它在故事板中一样。它不是空白,而是没有填充数据。

谁能告诉我我做错了什么?

更新 **** 10.10.2014 *******

这里可能所有的代码都会有所帮助:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell";

    JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell=[[JsonTableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *text; text = [NSMutableString stringWithFormat:@"%@", [tmpDict objectForKeyedSubscript:title]];

    NSMutableString *detail; detail = [NSMutableString stringWithFormat:@"%@ ", [tmpDict objectForKey:content]];

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc]initWithData:data];

    NSString *titleStrip = [[text description] stringByReplacingOccurrencesOfString:@""withString:@""];

    NSString *detailStrip = [[detail description] stringByReplacingOccurrencesOfString:@""withString:@""];

    dispatch_sync(dispatch_get_main_queue(), ^{

    cell.titreNews.text = titleStrip;

    cell.detailNews.textAlignment = NSTextAlignmentJustified;

    cell.detailNews.text = detailStrip;

    UIImage *image0 = [UIImage imageNamed:@"video.png"];

    if (img != nil) { cell.imageNews.image = img; } else { cell.imageNews.image = image0; }

    }); });

    [cell setAccessoryType:UITableViewCellAccessoryNone];

    return cell; }

【问题讨论】:

  • 您不能不对在 Storyboards 中创建的原型单元进行出列。您最后一行显示陈旧数据的原因是因为如果您的这部分代码:if (cell == nil) { cell.title.text = titleString }。这意味着您仅在分配新单元格时设置单元格标题文本(基本上跳过任何出列的单元格)。摆脱那个零检查,你应该很高兴。
  • 您好,感谢您的帮助,但它也不起作用

标签: objective-c xcode uitableview tableview


【解决方案1】:

这个代码块永远不会被调用,因为你在它之前实例化了一个单元格(单元格永远不会为零)。

if (cell == nil) {
    cell.title.text = titleStrip;
}

如果您不使用出列方法,请不要调用它们。将您的方法更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    //Don't even try to dequeue, just create new cell instance
    JsonTableViewCell *cell = [[JsonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.title.text = titleStrip;
    return cell;
}

【讨论】:

  • 感谢您花时间帮助解决我的问题,但这对我不起作用。如果我这样做,我到处都是空白单元格。
【解决方案2】:

首先,欢迎来到 Stack Overflow!

其次,我强烈建议不要禁用出队。 Apple 在预期行为方面是出了名的严格,尝试重新设计基本的 UI 元素(如 TableView)以不同的方式工作很容易让您的应用程序被拒绝。另外,出队是一件好事,它有助于降低内存使用量并总体上简化事情。找到一种方法来设计自己的代码要好得多,这样它就可以按照预期的方式与出队过程一起工作。

话虽如此,据我所知,您需要做的就是省略 dequeueReusableCellWithIdentifier 行,它应该可以工作。 cell 每次都是nil,因此每次都会从头开始重新初始化。

【讨论】:

  • Apple 不会拒绝不使表格视图单元出列的应用程序。这是一个完全可选的性能优化。
  • @FruityGeek 我完全同意,我的评论只是建议谨慎对待改变建议行为的心态。我在最后提供了答案,因为在这种特殊情况下,这很好。
  • @Nerrolken 感谢您的欢迎,我知道出队不是最好的做法,但是滚动时需要几秒钟才能更新的单元格不是很好。我只有 10 个单元,所以在 4S 上你经常会注意到这个问题。
猜你喜欢
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 2011-02-25
  • 2015-03-06
相关资源
最近更新 更多