Ios 重新使用带有imageview和按钮的单元格
我有一个包含许多单元格的表视图。在第一个单元格中,我放置了一个UIImageView来保存一张图片,旁边有一个按钮可以打开一个弹出窗口。 我的问题是:为什么在其他单元格正常工作的情况下,尽管检查了cell==nil,这个单元格仍被重用 代码如下 另外,这不需要使用故事板和xib,只需要编程Ios 重新使用带有imageview和按钮的单元格,ios,uitableview,Ios,Uitableview,我有一个包含许多单元格的表视图。在第一个单元格中,我放置了一个UIImageView来保存一张图片,旁边有一个按钮可以打开一个弹出窗口。 我的问题是:为什么在其他单元格正常工作的情况下,尽管检查了cell==nil,这个单元格仍被重用 代码如下 另外,这不需要使用故事板和xib,只需要编程 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0)
{
if(indexPath.row==0)
{
if(self.imageView==nil)
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 2, 50, 40)];
self.imageView.image = [UIImage imageNamed:@"user.png"];
self.imageView.backgroundColor=[UIColor blackColor];
}
[cell.contentView addSubview:self.imageView];
if(self.chooseImage==nil)
{
self.chooseImage = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 80, 30)];
[self.chooseImage addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
self.chooseImage.backgroundColor = [UIColor blackColor];
[self.chooseImage setTitle:@"CHOOSE" forState:UIControlStateNormal];
}
[cell.contentView addSubview:self.chooseImage];
}
首先在方法返回单元格的末尾添加代码;并使用以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(self.imageView==nil)
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 2, 50, 40)];
self.imageView.image = [UIImage imageNamed:@"user.png"];
self.imageView.backgroundColor=[UIColor blackColor];
}
[cell.contentView addSubview:self.imageView];
if(self.chooseImage==nil)
{
self.chooseImage = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 80, 30)];
[self.chooseImage addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
self.chooseImage.backgroundColor = [UIColor blackColor];
[self.chooseImage setTitle:@"CHOOSE" forState:UIControlStateNormal];
}
[cell.contentView addSubview:self.chooseImage];
}
return cell; // this line is not in your code so add this line.
}
你到底想知道什么。。你想检查你的电池是否被重复使用?这是正确的吗?这个特定的单元格只得到了重用。其他单元格都很好,所以我想知道为什么?您添加到显示中的行可能是如何显示的。i、 e-NSIntegertableView:UITableView*表格视图行数Section:NSIntegersection这是我的行代码Section-NSIntegertableView:UITableView*表格视图行数Section:NSIntegersection{//警告方法实现不完整//返回节中的行数。如果节==0,则返回5;如果节==1,则返回4;如果节==2,则返回4;如果节==3,则返回2;返回-1;}我有很多部分,它的行都运行良好,但只有这一行故意给我造成了问题,因为我下面有更多的单元格:-@HarshitK这是如何在单元格上添加控件的结构:我想我找到了答案,我删除了单元格==nil,所有都按我想要的方式运行!