xcode 如何不使用 dequeueReusableCellWithIdentifier?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26225882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:47:52  来源:igfitidea点击:

How to NOT use dequeueReusableCellWithIdentifier?

objective-cxcodeuitableviewtableview

提问by Olico

I have a small table view, maybe 10 items max, and I do not want to use the dequeueReusableCellWithIdentifiermethod. The reason is, each time I'm scrolling the table view, I have the first row in place of the last row, which take few second to update back to get update back with the last row.

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

I'm using a prototype cell and i try to remove the :

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

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

but it return blank cells in table view.

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

So here is my code :

所以这是我的代码:

- (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];
}

I also try to keep the

我也尽量保持

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

and configure the cell like:

并配置单元,如:

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

but in this case, I've got the prototype cell as it is in the storyboard. It's not blank, but not filled with data.

但在这种情况下,我有原型单元,因为它在故事板中。它不是空白,而是没有填充数据。

Anybody can tell me what I am doing wrong?

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

UPDATE **** 10.10.2014 *******

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

Here is maybe the all code would help :

以下是所有代码可能会有所帮助:

- (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; }

回答by Nerrolken

First of all, welcome to Stack Overflow!

首先,欢迎来到 Stack Overflow!

Second, I highlyrecommend against disabling dequeuing. Apple is famously strict when it comes to expected behaviors, and trying to re-work a basic UI element like a TableView to work in a different way could easily get your app rejected. Plus, dequeuing is a good thing, which helps keep memory usage down and streamlines things in general. It's far, far better to find a way to design your own code so it works with the dequeuing process as it is intended to work.

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

That being said, as I understand it all you need to do is omit the dequeueReusableCellWithIdentifierline, and it should work. cellwill be nileach time, and will thus get re-initialized each time from scratch.

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

回答by Fruity Geek

This block of code is never called because you instantiate a cell before it (cell is never nil).

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

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

Don't call the dequeue methods if you aren't using them. Change your method to this:

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

- (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;
}