dequeueReusableCellWithIdentifier创建重复的UITableviewcell

时间:2017-11-16 08:44:03

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

View I a creating

我正在创建一个我正在使用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中,当我们点击城市时,会打开一个新的控制器,我可以选择城市,它将反映在当前viewcontroller的City Column中。

现在问题。
当我用这个单元格打开页面时,会创建新的单元格,但是在点击城市控制器并选择城市后,它会转到此前一个控制器,而不是使用相同的单元格,而是创建新的单元格。所以它就像一个单元格而不是另一个单元格(Duplicates)

现在我再次点击城市控制器并选择城市,选择此VC时它会使用2个视图中的隐藏视图。

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

#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 
相关问题