好的我在每个单元格中创建了一个带有单选按钮的简单表格视图,这样做是为了查看单元格重复的原因。我将我的行设置为一个非常高的计数,以显示细胞确实重复。这个简单项目的目标是在解决这个问题时得出一个合理的结论,因为在这个主题上有几个帖子没有给出正确的结果。当用户在单元格中选择该单元格中的按钮时,该单元格应该受到影响。这是完整的代码。
#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];
}
}
答案 0 :(得分:1)
您正在重复使用单元格,因此如果不更改内容,则会看到相同的单元格出现在其他行中。由于您只在分配单元格时设置内容,因此在重复使用单元格时内容将保持不变
所以
//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;
如果你想根据行改变单元格内容,你应该在你的单元格== nil块之后这样做
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;
希望这会有所帮助..
答案 1 :(得分:1)
Daniels的回答帮助我解决了一些问题,以解决我的问题。我的问题是我看到sections 0,1,2
是唯一创建的,但second 3
生成的内容与section 0
相同,这是我的设置:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"cell";
CustomCell* cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:[NSString stringWithFormat:@"SECTION %d", indexPath.section]];
NSLog(@"SECTION %d", indexPath.section);
return cell;
}
我需要更改的是cellIndentifier
,因为我将它们全部设置为相同的ID:
NSString *cellIdentifier = [NSString stringWithFormat:@"cell-%d", indexPath.section];