2014-12-09 10 views
0

У меня есть один пользовательский UITableViewCell с xib.Пользовательский UITableViewCell XIB повторяет строку после прокрутки

ProductCell

И еще ViewController с Tableview.

Теперь я хочу добавить ячейку в таблицу.

Вот мой код.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.tblList.dataSource = self; 

    [self.tblList registerNib:[UINib nibWithNibName:@"ProductCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MyIdentifierList"]; 

//other code lines apart from tableview. 
} 

TableView Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.aryData count]; 
} 


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 100.0; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierList"]; 

    NSDictionary *dict = [self.aryData objectAtIndex:indexPath.row]; 
    cell.lblProductName.text = [dict objectForKey:@"Product"]; 

    cell.tag = indexPath.row; 

    NSLog(@"CELL : %@",cell); 

    return cell; 
} 

данных консоли, которые вы увидите, что экземпляр повторяются. (Это вызывает проблему с флажком ячейки и значением UIstepper ячейки, которые являются дочерними элементами ячейки).

2014-12-09 18:45:43.619 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b804b90; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; layer = <CALayer: 0x7f9d994b6330>> 
2014-12-09 18:45:43.622 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d99663780; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7f9d99664b90>> 
2014-12-09 18:45:43.625 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a5870; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 2; layer = <CALayer: 0x7f9d996a56b0>> 
2014-12-09 18:45:43.628 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b817e50; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 3; layer = <CALayer: 0x7f9d9b817c90>> 
2014-12-09 18:45:43.630 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b81e810; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 4; layer = <CALayer: 0x7f9d9b81e650>> 
2014-12-09 18:45:43.633 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b825140; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 5; layer = <CALayer: 0x7f9d9b824fd0>> 
2014-12-09 18:45:54.978 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a7400; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 6; layer = <CALayer: 0x7f9d996b4520>> 
2014-12-09 18:45:55.241 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b804b90; baseClass = UITableViewCell; frame = (0 0; 375 100); hidden = YES; autoresize = W; tag = 7; layer = <CALayer: 0x7f9d994b6330>> 
2014-12-09 18:45:56.824 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d99663780; baseClass = UITableViewCell; frame = (0 100; 375 100); hidden = YES; autoresize = W; tag = 8; layer = <CALayer: 0x7f9d99664b90>> 
2014-12-09 18:45:57.045 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a5870; baseClass = UITableViewCell; frame = (0 200; 375 100); hidden = YES; autoresize = W; tag = 9; layer = <CALayer: 0x7f9d996a56b0>> 

ProductCell класс XIB не в раскадровке, но его отдельно.

ответ

0

Это нормальное поведение. UITableView повторно использует ячейки по соображениям производительности. Вы на самом деле сказал это, чтобы сделать это самостоятельно, здесь:

ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierList"]; 

Уведомление dequeueReusable в dequeueReusableCellWithIdentifier: - этот метод повторно ячейку из очереди вашего Tableview-х, когда это возможно.

Я не вижу, что происходит с вашим шагомпом и флажком из кода, который вы опубликовали, но то, что вам, вероятно, нужно сделать, это установить их значения в tableView:cellForRowAtIndexPath:.

+0

Кроме того, в файле .m файла ProductCell вы можете реализовать 'prepareForReuse', в котором вы можете вернуть ячейку в состояние по умолчанию. – BreadicalMD

+0

Здесь Повторяющаяся ячейка означает, что у меня есть 50 строк. Сначала она отображает 7 строк, как только я начинаю прокрутку, у меня есть тот же экземпляр назад. Таким образом, не удалось установить правильные значения. См. Этот экземпляр: 0x7f9d9b804b90 повторяется после 7 строк. –

+0

Да, и это то, что должно произойти. Почему это означает, что вы не можете установить правильные значения для вашего Stepper и Checkbox? У вас есть магазины для них? – JoeFryer