好吧,我創建了一個單選按鈕在每個單元格中的簡單表格視圖,這是爲了瞭解爲什麼單元格重複。我把我的行數設置得很高,表明單元格確實重複了。這個簡單的項目的目標是在解決這個問題時得出一個合理的結論,因爲在這個主題上有幾篇文章沒有給出正確的結果。當用戶在單元格中選擇一個按鈕時,只有該單元格應該受到影響。這是整個代碼。UItableViewCells重複
#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 [email protected]"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];
}
}
什麼不行? – Mundi 2012-07-26 17:50:46
究竟是什麼問題?單元格不會在tableViews中重複使用,您正在重用單元格。 – ohr 2012-07-26 17:57:19
你覺得'dequeueReusableCellWithIdentifier:'中的'Reusable'這個詞是什麼意思? – 2012-07-26 18:03:55