2014-12-09 24 views
0

我有一個xib自定義UITableViewCell。滾動後自定義UITableViewCell XIB重複行

ProductCell

和Tableview的另一個視圖控制器。

現在我想將Cell添加到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的數據源

- (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 class xib不在Storyboard中,但是它是單獨的。

回答

0

這是正常行爲。由於性能原因,UITableView重複使用單元格。實際上,你已經告訴它該做自己,在這裏:

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

通知dequeueReusabledequeueReusableCellWithIdentifier: - 這種方法重用從您的tableView的隊列中的電池在可能的情況。

我無法看到您的步進器和您發佈的代碼中的複選框發生了什麼,但您可能需要做的是將其值設置爲tableView:cellForRowAtIndexPath:

+0

此外,在ProductCell的.m文件中,您可以實現'prepareForReuse',您可以在其中將單元返回到其默認狀態。 – BreadicalMD 2014-12-09 16:55:32

+0

這裏重複單元格意味着我有50行,首先它顯示7行,一旦我開始滾動,我得到相同的實例。所以不能設定正確的值。 看到這個實例:0x7f9d9b804b90在7行之後重複。 – 2014-12-09 17:52:25

+0

是的,那就是應該發生的事情。爲什麼這意味着您無法爲您的步進器和複選框設置適當的值?你有他們的網點嗎? – JoeFryer 2014-12-10 10:34:51