ios - dequeueReusableCellWithIdentifier 创建重复的 UITableviewcell

标签 ios objective-c iphone uitableview cocoa-touch

View I a creating

我正在创建一个使用 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/

相关文章:

ios - 关闭 ViewController 后,我的 View 保持在 iOS 7 中以前的 View Controller 方向

objective-c - Swift 应用迁移问题

iphone - 异步调用(特别是 iOS 地理编码)

ios - 为什么 UIScrollView 的内容不居中?

iOS 状态恢复

objective-c - 在 WatchKit 的 presentControllerWithNames 中将自身作为上下文传递

iphone - 使用麦克风音量作为触发器的动态动画

iphone - 如何在 iOS 5 中运行多个循环和条件检查?

ios - 如何隐藏状态栏+在另一个 View Controller (swift)中使用白光内容?

objective-c - 您如何获得 KVO 的可观察键路径列表?