我正在创建一个使用 UITableView 和 UITableViewCell 的页面。上面显示的照片是一个以编程方式创建的 UITableViewCell。
这个 UITableViewCell 是在 cellForRowAtIndexPath 中创建的,我在其中为 View 使用标签,并在调用 cellForRowAtIndexPath 时重复使用带有标签的 View
-------------------- 单元格创建代码------------------------ ---
NSString* reuseIdentifier = @"NameNCityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// Configure the cell...
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
[self configureCell:cell withIndexPath: indexPath];
在这个 UITableViewCell 中,当我们点击城市时,会打开一个新的 Controller ,我可以在其中选择城市,这将反射(reflect)在当前 View Controller 的城市列中。
现在问题来了。
当我打开带有这个单元格的页面时,会创建新的单元格,但是在点击 citycontroller 并选择城市后,它会转到之前的 Controller ,而不是使用相同的单元格,而是创建新的 .所以它就像一个单元格覆盖另一个单元格(重复)
现在我再次点击 citycontroller 并选择 city,当涉及到这个 VC 时选择它,它使用 2 个 View 中的隐藏 View 。
我需要知道我应该怎么做才能不创建两个 View 。我想使用相同的 View 。
如果您需要更多详细信息,请立即联系我。
最佳答案
You are overwriting cell in a UItableview. Please use below simple 1 line of code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
YourtableViewCellName *cell = (YourtableViewCellName *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.profilePic.image = [UIImage imageNamed:@"Image-1"]; //profilePic - image View
cell.titleTV.text = @"Hi"; //Name
cell.detailTV.text = @"How are you?"; //City
return cell;
}
关于ios - dequeueReusableCellWithIdentifier 创建重复的 UITableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47325108/