Ios TableView的每个单元格都填充了相同的数据集
我有一个自定义单元格的uitableview 在Ios TableView的每个单元格都填充了相同的数据集,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个自定义单元格的uitableview 在cellforrowatinexpath方法中,im从核心数据中获取值并显示它 它必须唯一地显示数据,但问题是tableview的每个单元格都显示相同的数据集 下面是我的cellforrowatinexpath方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableV
cellforrowatinexpath
方法中,im从核心数据中获取值并显示它
它必须唯一地显示数据,但问题是tableview的每个单元格都显示相同的数据集
下面是我的cellforrowatinexpath
方法
- (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.productAmountTextLabel = [[UILabel alloc]init];
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
self.productAmountTextLabel.text = [device valueForKey:@"amount"];
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.productAmountTextLabel];
return cell;
}
“金额”标签仅从db中获取第一个值,并在所有单元格中显示第一个值。我怎样才能解决这个问题呢?试试这个代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"any-cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"any-cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
UILabel* productAmountTextLabel = [[UILabel alloc]init];
productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
productAmountTextLabel.frame = CGRectMake(0, 0, 100, 30); // for example
productAmountTextLabel.tag = 10000; // any number
[cell.contentView addSubview:productAmountTextLabel];
}
// tableView.tableFooterView = [UIView new];
UILabel* lbl = (UILabel*)[cell.contentView viewWithTag: 10000];
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
lbl.text = [device valueForKey:@"amount"];
return cell;
}
- (void) makeFooter {
myTableView.tableFooterView = [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.textLabel.text = [device valueForKey:@"amount"];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.devices count];
}
问题可能是您正在使用的全局UILabel。如果要在标签中进行任何自定义,请创建自定义UITableViewCell类并在其中声明标签。声明单元格时使用类而不是UITableViewCell请尝试以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.textLabel.text = [device valueForKey:@"amount"];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.devices count];
}
问题可能是您正在使用的全局UILabel。如果要在标签中进行任何自定义,请创建自定义UITableViewCell类并在其中声明标签。在声明单元格时使用类而不是UITableViewCell此问题通常发生在为行和节返回值时出错 请检查您的密码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.textLabel.text = [device valueForKey:@"amount"];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.devices count];
}
可能您返回了值,反之亦然当您为行和节返回值时出错,通常会出现此问题 请检查您的密码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.textLabel.text = [device valueForKey:@"amount"];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.devices count];
}
可能您已返回值,反之亦然不要使用ivar引用标签,只需使用局部变量,也不要在该数学中创建页脚。请参考我的代码进行详细说明?谢谢,您可以直接使用cell.textLabel而不是self.ProductAmountTextLabel,或者创建一个自定义表视图单元格并在其中创建标签并使用它。问题在于您的标签是全局声明的
单元格。productAmountTextLabel
不可能,因为在类型为UITableView
的对象上找不到属性productAmountTextLabel
,请不要使用ivar引用标签,只使用局部变量,也不要在这个数学模型中创建页脚。请详细说明我的代码?谢谢,您可以直接使用cell.textLabel而不是self.ProductAmountTextLabel,或者创建一个自定义表视图单元格并在其中创建标签并使用它。问题是您的标签全局声明为单元格。productAmountTextLabel
不可能,因为在类型为UITableView
的对象上找不到属性productAmountTextLabel
,我尝试了它,但仍然存在相同的问题我尝试了记录NSLog(@“%@,[device valueForKey:@“amount]”)代码>并且它只打印一个值,这是第一个值我认为您的数组不正确(@“ds%@”,amountArray);包含两个唯一的值请检查“self.devices”和“[device valueForKey:@“amount”]”的值。我尝试了它,但仍然存在相同的问题我尝试了记录NSLog(@“%@“,[device valueForKey:@“amount”])代码>并且它只打印一个值,这是第一个值我认为您的数组不正确(@“ds%@”,amountArray);包含两个唯一的值请检查“self.devices”和“[device valueForKey:@“amount”]”的值。仍然存在相同的问题。兄弟。能否检查self.devices数组中的值是否正确?self.devices数组中的值是否正确,但NSLog(@“%@,[device valueForKey:@“amount”])代码>只打印一个值两次,因为有两个单元格,直到相同的问题仍然存在bro。您可以检查self.devices数组中的值是否正确吗?self.devices数组中的值正确,但NSLog(@“%@,[device valueForKey:@“amount”)代码>只打印一个值两次,因为有两个单元格eah..我通过日志调试它是..我通过日志调试它