如何不使用dequeueReusableCellWithIdentifier?

Oli*_*ico 6 xcode objective-c uitableview tableview

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

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

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

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

所以这是我的代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我也试着保持

JsonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

并配置单元格如:

if (cell == nil) {
    cell.title.text = titleStrip;
}
Run Code Online (Sandbox Code Playgroud)

但是在这种情况下,我在故事板中得到了原型单元格.它不是空白,但没有填充数据.

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

更新****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; }

Ner*_*ken 3

首先,欢迎来到 Stack Overflow!

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

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

  • Apple 不会拒绝不使表视图单元格出队的应用程序。这是完全可选的性能优化。 (7认同)