【问题标题】:dequeueReusableCellWithIdentifier creates duplicate UITableviewcelldequeueReusableCellWithIdentifier 创建重复的 UITableviewcell
【发布时间】:2018-04-29 17:20:59
【问题描述】:

我正在创建一个使用 UITableView 和 UITableViewCell 的页面。上图是一个以编程方式创建的 UITableViewCell。

这个 UITableViewCell 是在 cellForRowAtIndexPath 中创建的,我在其中使用视图标签并在调用 cellForRowAtIndexPath 时重用带有标签的视图

-------------------- 单元格创建代码 ------------------------ ---

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 中,当我们点击 city 时,会打开一个新控制器,我可以在其中选择城市,这将反映在当前视图控制器的 City Column 中。

现在问题来了。
当我打开带有此单元格的页面时,会创建新单元格,但是在点击 citycontroller 并选择 city 后,它会进入前一个控制器,而不是使用相同的单元格,而是创建新的 .所以它就像一个单元格在另一个单元格之上(重复)

现在我再次点击 citycontroller 并选择城市,当涉及到这个 VC 时,它会使用来自 2 个视图的隐藏视图。

我需要知道我应该怎么做才能不创建两个视图。我想使用相同的视图。

如果您需要更多详细信息,请立即联系我。

【问题讨论】:

  • 你不需要整个if (nil == cell)....部分,删除它,对于你的问题,你应该在这里发布单元格点击处理功能
  • 然后每次创建一个新单元格
  • @SudhanshuGupta 你能在选择城市和点击单元格时显示代码吗?
  • configureCell:withIndexPath: 该方法的代码是什么?

标签: ios objective-c iphone uitableview cocoa-touch


【解决方案1】:

创建自定义单元格类,如示例

#import "JSCustomCell.h"
@implementation JSCustomCell
@synthesize descriptionLabel = _descriptionLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // configure control(s)
        self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 300, 30)];
        self.descriptionLabel.textColor = [UIColor blackColor];
        self.descriptionLabel.font = [UIFont fontWithName:@"Arial" size:12.0f];

        [self addSubview:self.descriptionLabel];
    }
    return self;
}
@end

然后是主视图控制器

#import "JSViewController.h"
#import "JSCustomCell.h"
@interface JSViewController ()
@end
@implementation JSViewController {
    UITableView *tableView;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // init table view
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    // must set delegate & dataSource, otherwise the the table will be empty and not responsive
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor cyanColor];
    // add to canvas
    [self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}
// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"HistoryCell";

    // Similar to UITableViewCell, but 
    JSCustomCell *cell = (JSCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[JSCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // Just want to test, so I hardcode the data
    cell.descriptionLabel.text = @"Testing";

    return cell;
}
#pragma mark - UITableViewDelegate
// when user tap the row, what action you want to perform
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected %d row", indexPath.row);
}
@end 

【讨论】:

    【解决方案2】:
        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;
         }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多