加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何不使用dequeueReusableCellWithIdentifier?

发布时间:2020-12-14 17:57:05 所属栏目:百科 来源:网络整理
导读:我有一个小表视图,最多可能有10个项目,我不想使用dequeueReusableCellWithIdentifier方法.原因是,每次我滚动表视图时,我都会有第一行代替最后一行,这需要几秒钟的时间来更新以获得最后一行的更新. 我正在使用原型单元,我尝试删除: JsonTableViewCell *cell
我有一个小表视图,最多可能有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; }

解决方法

永远不会调用此代码块,因为您在它之前实例化一个单元格(单元格永远不是nil).

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读