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中,但是它是單獨的。
此外,在ProductCell的.m文件中,您可以實現'prepareForReuse',您可以在其中將單元返回到其默認狀態。 – BreadicalMD 2014-12-09 16:55:32
這裏重複單元格意味着我有50行,首先它顯示7行,一旦我開始滾動,我得到相同的實例。所以不能設定正確的值。 看到這個實例:0x7f9d9b804b90在7行之後重複。 – 2014-12-09 17:52:25
是的,那就是應該發生的事情。爲什麼這意味着您無法爲您的步進器和複選框設置適當的值?你有他們的網點嗎? – JoeFryer 2014-12-10 10:34:51