Ios 表视图的每个单元格多次显示相同的值
下面是我的cellForRowAtIndexPath方法Ios 表视图的每个单元格多次显示相同的值,ios,uitableview,Ios,Uitableview,下面是我的cellForRowAtIndexPath方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];
- (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]时,它应该显示两个唯一的值,但它多次显示相同的值。我甚至打了个圈
“环绕表”视图,检查它是否有两个值,但在显示它时,相同的值会显示多次。您需要像这样添加dequeueReusableCellWithIdentifier -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { }请执行以下代码:
-(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中设置标识符。在
UITableViewCell
上执行addSubview
时的问题是,调用cellForRow:atIndexPath
时会多次添加标识符
您可以通过两种方式解决此问题:
cellForRow
中执行addSubview
cellForRow
中的所有子视图,如下所示:
for (UIView *sView in cell.contentView.subviews) {
[sView removeFromSuperview];
}
(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
}
您没有添加dequeueReusableCellWithIdentifier。只需将单元格=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“identifier”];在该方法中本地创建productAmountLabel。不要使其成为全局性的。@SRNayak您能解释一下吗?
“无法将标识符为的单元格出列-必须为标识符注册nib或类,或者在情节提要中连接原型单元格”***第一次抛出调用堆栈:
您可以像这样在viewdidload方法中注册[self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:@“reuseid”];还是同样的问题你能在本地创建所有标签吗?你能把这封邮件上的代码发给我吗imindpixel@gmail.comWhich你试过了吗?我试过这两种方法,但同样的问题仍然存在,兄弟
- (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
}