gpt4 book ai didi

ios - UItableViewCells 正在重复

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:49 24 4
gpt4 key购买 nike

好的,我创建了一个简单的表格 View ,每个单元格中都有一个单选按钮,这样做是为了查看单元格重复的原因。我将 Rows 设置为高得离谱,以表明单元格确实会重复。这个简单项目的目标是在解决这个问题时得出一个合理的结论,因为有几篇关于这个主题的帖子没有给出正确的结果。当用户在单元格中选择一个按钮时,该单元格并且只有该单元格应该受到影响。这是完整的代码。

    #import "faQViewController.h"

@interface faQViewController ()

@end

@implementation faQViewController
@synthesize button1,button2;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 30;


}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =@"cell";
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(0, 0, 22, 32);
[button1 setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell ==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.contentView
addSubview:button1];
}

// cell.imageView.image = [UIImage imageNamed:@"radioOff.png"];


return cell;
}

-(IBAction)buttonPressed:(id)sender{
if ([sender imageForState:UIControlStateNormal ]== [UIImage imageNamed:@"radioOff.png"]){
[sender setImage:[UIImage imageNamed:@"radioOn"] forState:UIControlStateNormal];
}else {

[sender setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];
}

}

最佳答案

您正在重复使用这些单元格,因此如果您不更改内容,您会看到其他行出现相同的单元格。由于你只是在分配单元格时设置了内容,所以当单元格被重用时内容将保持不变

所以

  //Here you tell the tableView to re use a cell if one is available for reuse
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//if the cell is nil (none available for reuse)
if (cell ==nil) {
//you create the cell and set its content
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.contentView
addSubview:button1];
}
//return the cell
return cell;

如果你想根据行更改单元格内容,你应该在你的 cell==nil block 之后这样做

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//if the cell is nil (none available for reuse)
if (cell ==nil) {
//you create the cell and set its content
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
] autorelease];
}

//set the content
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.contentView
addSubview:button1];

//return the cell
return cell;

希望这对您有帮助..

关于ios - UItableViewCells 正在重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11675033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com