【发布时间】:2012-07-25 09:04:36
【问题描述】:
好的,我在每个单元格中创建了一个带有单选按钮的简单表格视图,这样做是为了查看单元格重复的原因。我将我的行数设置为高得离谱,以表明细胞确实重复。这个简单项目的目标是在解决这个问题时得出一个合理的结论,因为有几个关于这个主题的帖子都没有给出正确的结果。当用户在单元格中选择一个按钮时,该单元格并且只有该单元格应该受到影响。这是完整的代码。
#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];
}
}
【问题讨论】:
-
到底是什么问题?单元格在 tableViews 中不重复,您正在重复使用单元格。
-
您认为
dequeueReusableCellWithIdentifier:中的“可重用”一词是什么意思? -
这不是我第一次使用 tableViews,从它的外观来看,单元格正在被重用,问题是,假设我在单元格中有一个可编辑的对象,即按钮的图像。如果用户更改此可编辑对象,即图像,为什么当用户滚动出视图时单元格会重新加载并删除已编辑的图像。以及它不改变的解决方案是什么。
标签: ios uitableview reload