2011-10-31 2 views
0

섹션 화 된 tableview 셀 안에 두 개의 단추가 있으며 엄지 손가락을 아래로 내립니다. 처음에는 두 버튼의 이미지가 선택되지 않습니다. 위로 엄지 위로 단추를 선택할 때 그것의 심상은 선정한 엄지 위로되고 다른 하나는 엄지 손가락으로 아래로 선택되고 그 반대도 마찬가지입니다.문제 단추 UITableView에 대한 태그를 설정하는 중

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"mod:numberOfSectionsInTableView"); 
    NSLog(@"[preferences count]=%d",[preferences count]); 
    return [preferences count]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.choices count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexpath 
{ 
    NSLog(@"cellForRowAtIndexPath: DISPLAY TEST"); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     NSLog(@"Text is: %@", [choices objectAtIndex:indexpath.row]);     
     NSLog(@"CHOICE AT INDEX PATH IS: %@",[choices objectAtIndex:indexpath.row %[choices count]]); 
     cell.textColor = [UIColor whiteColor]; 
     cell.backgroundColor = [UIColor blackColor]; 


     // Thumbs up button. 
     //UIButton *thumbsUp = [[UIButton alloc]init]; 
     thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];       
     [thumbsUp setTag:(indexpath.row+(indexpath.section * 50))]; 
     [thumbsUp addTarget:self action:@selector(thumbUp_ButtonClicked:event:) 
      forControlEvents:UIControlEventTouchUpInside];  
     [thumbsUp setTitle:@"" forState:UIControlStateNormal]; 
     thumbsUp.frame = CGRectMake(150.0, 20, 20, 15); 
     [thumbsUp setBackgroundImage:[UIImage imageNamed:@"thumbsup_not_selected.png"] 
    forState:UIControlStateNormal]; 

     //NSLog(@"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50))); 
     [cell.contentView addSubview:thumbsUp]; 

     // Thumbs down button 

     thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [thumbsDown addTarget:self action:@selector(thumbDown_ButtonClicked:event:) 
      forControlEvents:UIControlEventTouchUpInside]; 
     [thumbsDown setTitle:@"" forState:UIControlStateNormal]; 
     thumbsDown.frame = CGRectMake(200, 20, 20, 15); 

     [thumbsDown setTag:indexpath.row+120]; 
     [cell.contentView addSubview:thumbsDown]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 


     [thumbsDown setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"] 
      forState:UIControlStateNormal]; 
    } 

    NSLog(@"------------> TAG TEST %d",thumbsUp.tag); 
    cell.text = [choices objectAtIndex:(indexpath.row % [choices count])]; 


    NSLog(@"HELLO FOR TESTING"); 
    return cell; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    // Create label with section title 
    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.frame = CGRectMake(15, 10, 300, 25); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor blackColor]; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.textAlignment = UITextAlignmentLeft; 
    label.text = sectionTitle; 


    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)]; 
    [view autorelease]; 
    [view addSubview:label]; 
    //[view addSubview:segmentedControl]; 
    view.backgroundColor = [UIColor grayColor]; 
    return view; 
} 

//Thumbs Up Button Action 

- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event 
{ 
    NSLog(@"Thumbs Up Check!"); 


    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *) [[button superview] superview]; 


    NSIndexPath *indexPath = [myTable indexPathForCell:cell]; 
    NSLog(@"indexpath =%d",indexPath.row); 
    //[button setTag:indexPath.row+(indexPath.section * 50)]; 

    int cTag = [sender tag]; 
    NSLog(@"------>TAG : %d", cTag); 
    NSLog(@"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50)); 
    if(cTag == (indexPath.row+(indexPath.section * 50))) 
    { 
     NSLog(@"BUTTON COUNT:"); 
     [button setBackgroundImage:[UIImage imageNamed:@"thumbsup_selected.png"] 
      forState:UIControlStateNormal]; 
    } 


    NSInteger section = indexPath.section; 
    NSInteger row = indexPath.row; 

    //int row = button.tag; 

    NSLog(@"SECTION IS:%d",section); 
    NSLog(@"ROW IS: %d",row); 


    NSArray *array = cell.contentView.subviews; 
    NSLog(@"NUMBER OF OBJECTS: %d",[array count]); 
    UIButton *test = (UIButton *)[array objectAtIndex:2]; 

    [test setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]forState:UIControlStateNormal]; 

} 

하나의 버튼의 이미지를 변경하는 동안 버튼의 태그 문제로 인해 여러 버튼이 변경되고 있습니다. 어떤 사람이 해결책을 찾으면 도움이 될 것입니다 .... 태그는 우리가 볼 수있는 섹션의 버튼에 대한 설정입니다.

답변

0

if (cell == nil)의 바깥 쪽과 안쪽에 단추를 만들어 문제를 해결했습니다. 또한 단추의 현재 상태를 유지하기 위해 변경할 수있는 사전을 만들었습니다 .....

+0

셀이 재활용/재사용 될 때 이미 존재하는 단추를 다시 만들고 많은 스크롤을 한 후에 같은 셀에 많은 쓸모없는 단추가 있습니다. (모든 단추는 정확히 같은 위치와 크기로 표시되므로 마지막 단추 하나만 보이지만 추가로 많은 메모리와 렌더링 시간을 낭비합니다.) 다시 한 번, 모든 것이 설명 된 Table View 프로그래밍 가이드를 읽으십시오. 솔루션은 문제를 숨기지 만 해결하지 못하기 때문입니다. – AliSoftware

2

이유는

이동 애플의 문서에서 테이블보기 프로그래밍 가이드를 참조 (및 SO 검색 및의 웹 (... jQuery과에 대한 질문의 75 %에서와 같이) 재활용/재사용 메커니즘의 나쁜 사용하는 것입니다 테이블 뷰 및 재사용 메커니즘과 관련된 질문)

관련 문제