2014-04-09 3 views
3

가능한 중복있는 tableView 셀 버튼 이미지를 변경하려면 다음어떻게 IOS에서 클릭에

how to change tableView Cell button on click in iphone

나는 배경 이미지와 테이블 뷰 셀에 버튼이 기능을 사용하여 설정해야합니다. 도청 버튼에 대한

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SectionsTableIdentifier]; 

    cell.textLabel.text = self.names[self.keys]; 
    if (cell.accessoryView == nil) { 
     UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"]; 
     UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button setBackgroundImage:buttonUpImage 
          forState:UIControlStateNormal]; 
     [button setBackgroundImage:buttonDownImage 
          forState:UIControlStateHighlighted]; 
     [button setTitle:@"Send" forState:UIControlStateNormal]; 
     [button sizeToFit]; 
     [button addTarget:self 
        action:@selector(tappedButton:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = button; 

    } 



    return cell; 
} 

코드입니다 : 내가 필요로 이제

- (void)tappedButton:(UIButton *)sender 
{ 
    NSInteger row = sender.tag; 
    // NSString *character = self.names[self.keys]; 
    /* NSString *key = self.keys[indexPath.row]; 
    NSArray *nameSection = self.names[key]; 

    NSString *character = nameSection[indexPath]; 
    */ 


    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Request Notification." 
          message:[NSString 
            stringWithFormat:@"Friend Request Sent ."] 
          delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 

    /* UIImage *buttonUpImage = [UIImage imageNamed:@"button.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [cell.theSyncButton setImage:buttonUpImage forState:UIControlStateSelected]; 
    [button setBackgroundImage:buttonUpImage 
         forState:UIControlStateNormal]; 

    [button setTitle:@"Sent" forState:UIControlStateNormal]; 
    [button sizeToFit]; 
    */ 
} 

배경 색상과 제목을 변경하는 것입니다

동적에 사용자가 클릭이 테이블 뷰 셀에 '보내기'버튼을 때 "전송"을 괜찮나 전 이 기능을 사용할 수 있습니까? 버튼을 전달받을 tappedButton 당신의 IBAction를 방법에

+0

변경 버튼 배경 이미지 :'['사용 발신자 setBackgroundImage : buttonUpImage forState : UIControlStateNormal]' – Akhilrajtr

+0

[보낸 사람 setImage : buttonUpImage forState : UIControlStateSelected]; 여전히 작동하지 않습니다. – user3482051

+0

이미지를 설정 한 후'reloadRowsAtIndexPaths :'메소드로 셀을 다시로드하십시오. – Akhilrajtr

답변

1

그 (이하 "보낸 사람"매개 변수입니다.)

변경 버튼의 상태를 선택 "보낸 사람"

1

사용의 배경 색상 및 제목 탭 사용자,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [button setTitle:@"Send" forState:UIControlStateNormal]; 
    [button setBackgroundImage:original_image forState:UIControlStateNormal]; 
    [button setTitle:@"Sent" forState:UIControlStateSelected]; 
    [button setBackgroundImage:selected_image forState:UIControlStateSelected]; 
} 

tappedButton() 함수에서 선택된 [sender setSelected:YES]

주된 이유를 사용하는 송신기를 설정할 이 approch에 대해서는 isSelected 메서드를 사용하여 버튼의 상태를 감지 할 수 있습니다.

+0

이미지가 올바르게 바뀌고 있습니다. 그러나 클릭 이벤트 전후에 제목이 나타나지 않습니다 .. – user3482051

+0

@ user3482051 셀 초기화 섹션의 제목 및 이미지 코드 사용 및 동작 기능에서 [보낸 사람 setSelected : YES ] – demonofthemist

+0

죄송합니다. 어떤 작업 기능을 가리키는 지 불분명합니다 ... 위의 코드에서 분명하게 말하십시오 ... – user3482051

1
[sender setBackgroundColor:[UIColor whiteColor]]; 
((UIButton *)sender).titleLabel.text = @"title"; 
2

이 시도

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    .... 
    UIButton *button; 
    if (cell.accessoryView == nil) { 
     ... 
     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     .... 
     cell.accessoryView = button; 
    } 
    button.tag = indexPath.row; 

    ... 
} 

- (void)tappedButton:(UIButton *)sender 
{ 
    NSInteger row = sender.tag; 
    UIImage *buttonUpImage = [UIImage imageNamed:@"button.png"]; 
    [sender setBackgroundImage:buttonUpImage 
         forState:UIControlStateNormal]; 

    [sender setTitle:@"Sent" forState:UIControlStateNormal]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; 
    [self.tableView beginUpdates]; 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 

} 
+0

마지막 세 줄의 오류 "유형 테이블 컨트롤러의 객체에서 속성 테이블 뷰를 찾을 수 없습니다.": [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths : [NSArray arrayWithObjects : indexPath, nil] withRowAnimation : UITableViewRowAnimationFade]; [self.tableView endUpdates]; – user3482051

+0

'self.tableView'는 tableView를 참조합니다. (희망이있는 tableView는'IBOutlet'이 연결되어 있습니다.) 그렇다면 그 이름을'self '로 대체하십시오.tableView' – Akhilrajtr

2

안녕 당신이 할 수있는 tappedButton:에서 다음, tappedButton`에서

- (void)tappedButton:(UIButton *)sender 
{ 
    [sender setBackgroundColor:[UIColor grayColor]]; 
    [sender setTitle:@"Sent" forState:UIControlStateNormal]; 

    UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Request Notification." 
         message:[NSString 
           stringWithFormat:@"Friend Request Sent ."] 
         delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
    [alert show]; 
} 
관련 문제