2011-11-01 53 views
1

設置標籤的按鈕,我有內部切片的tableview細胞豎起大拇指兩個按鈕和拇指向下。最初沒有選擇兩個按鈕的圖像。當我選擇豎起大拇指按鈕時,它的圖像會變成拇指向上,而另一個則變成拇指向下,反之亦然。問題在切片的UITableView

//Number of sections 
- (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]; 
    }enter code here 


    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

您的問題可能事實上,只有在新創建單元時才設置變量。也許分區會影響有多少個單元受到影響。試着拉着你豎起大拇指/大拇指朝下碼出來的,如果(細胞==零)塊,看看是否有效果。

+0

感謝您的答覆。我嘗試了上述解決方案。但問題是,當滾動tableView更改按鈕的圖像..... – Susha