여러분, 저는 각 셀에 라디오 버튼이있는 간단한 테이블보기를 만들었습니다. 이것은 셀이 반복되는 이유를 확인하기 위해 수행되었습니다. 셀들이 실제로 반복된다는 것을 보여주기 위해 엄청나게 많은 수의 행을 설정했습니다. 이 간단한 프로젝트의 목표는이 주제에 대한 몇 가지 게시물이 없기 때문에이 문제를 해결할 때 올바른 결론을 내리는 것입니다. 사용자가 셀의 단추를 선택하면 해당 셀만 영향을받습니다. 다음은 코드 전체입니다.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
정확히 무엇이 문제입니까? 테이블 뷰에서 셀을 반복하지 않으면 셀을 재사용하고있는 것입니다. – ohr
'reusable'이라는 단어가'dequeueReusableCellWithIdentifier :'에서 무엇을 의미한다고 생각하니? –