2012-07-26 4 views
2

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

} 
+0

어떤 방식으로 작동하지 않습니까? – Mundi

+1

정확히 무엇이 문제입니까? 테이블 뷰에서 셀을 반복하지 않으면 셀을 재사용하고있는 것입니다. – ohr

+0

'reusable'이라는 단어가'dequeueReusableCellWithIdentifier :'에서 무엇을 의미한다고 생각하니? –

답변

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; 

당신은 당신이해야 할 행에 따라 셀의 내용을 변경하려는 경우 그래서 이후 휴대 == 전무 블록 그래서이 도움이

 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; 

희망 .. 내 문제를 해결하기 위해 최대

+0

그러나 이미지를 다시로드하므로 사용자가 이미지를 변경하기 위해 단추 이미지를 클릭하면보기가 아래로 스크롤되면 원래 이미지로 되돌아갑니다 – KING

1

다니엘스의 대답은 나에게 분명 일도 도왔습니다. 내 문제는 내가 여기, sections 0,1,2 고유하게 생성되고 있었다하지만 second 3section 0와 동일하게 생성되고 있었다보고 있었다되어 내 설정 : 내가 그들 모두를 설정 된 이후 cellIndentifier이었다 변경할 필요가 무엇

-(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; 
} 

동일한 ID :

NSString *cellIdentifier = [NSString stringWithFormat:@"cell-%d", indexPath.section]; 
관련 문제