以下是我的cellForRowAtIndexPath方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
self.productAmountLabel = [[UILabel alloc]init];
self.productAmountTextLabel = [[UILabel alloc]init];
if (IS_IPHONE_4_OR_LESS || IS_IPHONE_5)
{
self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);
}
else if (IS_IPHONE_6)
{
self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);
}
else if (IS_IPHONE_6P)
{
self.productAmountLabel.frame = CGRectMake(75, 145, 100, 30);
self.productAmountTextLabel.frame = CGRectMake(117, 145, 100, 30);
}
self.productAmountLabel.text = @"Amount:";
self.productAmountLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
// self.productAmountTextLabel.text = [NSString stringWithFormat:@"%@",amountArray[indexPath.row]];
// self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
self.productAmountTextLabel.text = amountArray[indexPath.row];
self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
tableView.tableFooterView = [UIView new];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
[cell.contentView addSubview:self.productAmountLabel];
[cell.contentView addSubview:self.productAmountTextLabel];
// cell.layer.shadowColor = [[UIColor blackColor] CGColor];
// cell.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
// cell.layer.shadowOpacity = 0.7f;
// cell.layer.shadowRadius = 4.0f;
//[cell.layer setMasksToBounds:YES];
return cell;
}
问题在于,当我说amountArray [indexPath.row]时,它应该显示两个唯一值,但它会多次显示相同的值。我甚至打过圈 在表视图周围检查它是否有两个值,但是在显示它时,打印相同的值会多次显示。
答案 0 :(得分:2)
请执行以下代码:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *indentifier = @"indentifier";
// Here I am creating cell in XIB -----------
Team_TableViewCell *Cell = (Team_TableViewCell *)[tableView dequeueReusableCellWithIdentifier:indentifier];
if (Cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Team_TableViewCell" owner:self options:nil];
Cell = [nib objectAtIndex:0];
}
// My required lable--------
Cell.titleLbl.text = reportees.name;
Cell.pointEarnedLbl.text = reportees.points;
Cell.titleLbl.textColor=APP_HOME_TABLE_HEADER_COLOR;
[Cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[Cell setAccessoryType:UITableViewCellAccessoryNone];
return Cell;
}
注意:如果您在XIB中创建,请在Tableview中设置标识符。
答案 1 :(得分:1)
您需要像这样添加dequeueReusableCellWithIdentifier
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=@"reuseid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellId];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
self.productAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];
self.productAmountTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,25, 100, 20)];
self.productAmountLabel.text = @"Amount:";
self.productAmountLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
self.productAmountTextLabel.text = [amountArray objectAtIndex:indexPath.row];
self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
tableView.tableFooterView = [UIView new];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
[cell.contentView addSubview:self.productAmountLabel];
[cell.contentView addSubview:self.productAmountTextLabel];
return cell;
}
答案 2 :(得分:1)
在addSubview
上执行UITableViewCell
时遇到的问题是,在调用cellForRow:atIndexPath
时会多次添加该问题。
您可以通过两种方式解决问题:
addSubview
中执行cellForRow
。在添加任何子视图之前删除cellForRow
中的所有子视图,如下所示:
for (UIView *sView in cell.contentView.subviews) {
[sView removeFromSuperview];
}
作为一个quickfix,方法2可行,但方法1更好,因为您避免不必要的添加/删除子视图。
修改强>
重新考虑时,还有第三种方式。之前初始化您的标签,并将标签添加为(cell == nil)
块中的子视图:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];
self.productAmountLabel = [[UILabel alloc]init];
self.productAmountTextLabel = [[UILabel alloc]init];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView addSubview: self.productAmountLabel];
[cell.contentView addSubview: self.productAmountTextLabel];
}
// remaining code, but do not call `addSubview` after this point
}