2017-11-16 6 views
0

View I a creatingdequeueReusableCellWithIdentifierは私がのUITableViewとのUITableViewCellを使用していますページを作成していますUITableViewCellの

を複製作成されます。上記の写真は、プログラムで作成された1つの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列に反映される都市を選択できる場所が開きます。

今問題です。
このセルでページを開くと、新しいセルが作成されますが、citycontrollerをタップして都市を選択すると、以前のコントローラに来て、同じセルを使用する代わりに新しいセルが作成されます。だから、そのような1つのセル上の別のセル(重複)

私はもう一度citycontrollerをタップし、このVCになると、2つのビューからの選択時に都市を選択します。

私は知っている必要がありますどのように2つのビューが作成されないように行う必要があります。私は同じ考え方で仕事をしたい。

さらに詳しい情報が必要な場合は、今すぐ教えてください。

+0

....'パートなどのカスタムセルクラスを作成します。それを削除してください。問題を解決するには、ここでセルタップ処理関数を投稿する必要があります – Tj3n

+0

新しいセルが作成されるたびに –

+0

@SudhanshuGupta都市を選択してceをタップするとコードが表示されます? – trungduc

答えて

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

これを使用するには、まず登録する必要がありますか?私は実際にcellforrowatindexのセルのためのビューを作成しています –

+0

あなたはInterface.Thenでカスタムセルを作成する必要がありますあなたはクラスでカスタムセルを作成する必要があります。セルの識別子を設定します。 –

+0

Renish Dadhaniya私はxibではなくプログラムでそれをやりたい –

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 
関連する問題