【发布时间】: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