2013-11-15 5 views
0

나는 Custom Cell과 Section Header가있는 TableView를 가지고있다. 헤더는 나에게 국가를 보여주고 사용자 지정 셀은 나에게 도시를 보여줍니다.커스텀 UITableViewCell에서 버튼 클릭을 결정하는 방법은 무엇입니까?

는 다음과 같이 :

미국에게

  • 뉴욕

  • 로스 앤젤레스

독일

  • 베를린

  • Frakfurt

모든 셀은 버튼을 얻었다. 언론을보고 나면 도시 이름을 탐색하여 상세보기로 푸시합니다.

문제는 버튼을 누른 후 도시 이름을 결정하는 방법을 모르겠다는 것입니다.

나는 해결책을했지만, 그것은 더 이상 작동하지 않습니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:@"ortCell"]; 
    [cell.detailButton setTag:indexPath.row]; 
    cell.ortLabel.text = [managedObject valueForKey:@"stadtname"]; 
} 


- (IBAction)detailButton:(id)sender { 

    UIView *contentView = [sender superview]; 
    UITableViewCell *cell = (UITableViewCell *)[contentView superview]; 
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; 

    NSInteger section = cellIndexPath.section; 

     UIButton * myButton = sender; 
    int row=myButton.tag; 

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 

    StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"strasse"]; 
    self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    detail.currentStandort = self.selectedStandort; 
    [self.navigationController pushViewController:detail animated:YES]; 

} 

난 내 예전의 코드를 사용할 때 그것은 단지 첫 번째 섹션에서 정확한 거리 행을 표시합니다. 그것은 다른 섹션 (국가)을 감지하지 못했습니다.

내가 thirt 섹션에서 첫 번째 도시를 선택했을 때. 그것은 첫 번째 섹션에서 첫 번째 도시를 표시합니다.

도와 주시면 감사하겠습니다.

+0

가능한 [iOS의 매개 변수로 UITableViewPathPath를 @ selector로 전달하는 방법] (http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector) -by-parameters-in-ios) – TheTiger

답변

0

당신은

태그 = 행 * 1000 + 섹션으로 태그를 만들기 때문에 별도로 행과 섹션의 정보를 보낼 필요가있다. 태그 가져 오기 섹션과 올바른 인덱스 경로를 만들거나 그것을 얻기 위해 2 분 후 더 좋은 해결책을 찾기 위해 행 = 태그/1000 섹션 = 태그 % 1000

사용이 값을 사용하여 행 값에서 지금

.

는주의의

  1. 서브 클래스있는 UIButton
  2. 가 indexpath 속성 추가를 int로
  3. 버튼 객체를 만들기 위해 클래스를 만들 재산 indexpath 설정 태그의 insted에 올바른 해결 방법이 될 것입니다. CustomButton에서
    @interface CustomButton :UIButton 
    @property (nonatomic,assign)NSIndexPath *path; 
    @end 
    

    CustomButton.h

    에서

.m

@implementation CustomButton 
@synthesize path; 
@end 

사용하여 테이블 뷰 셀이 사용자 정의 버튼 대신에 태그 설정 경로 속성

+0

예를 들어 주시겠습니까? –

+0

[cell.detailButton setTag : indexPath.row]; 이 라인 insted 사용 int int tag = indexPath.row * 1000 + indexPath.section, [cell.detailButton setTag : tag]; – amar

+0

대단히 감사합니다! –

1

사용 나는 그것이 당신의 목적을 해결할 희망이.

뷰 컨트롤러의 버튼을 누를 방법 만들기

  • (IBAction를) detailButton (있는 UIButton *) 송신기 {

    CustomUITableViewCellName * 셀 = (CustomUITableViewCellName *) [발신자 수퍼] 수퍼] ; NSIndexPath * cellIndexPath = [YOUR_TABLE_NAME indexPathForCell : cell]; IOS 7의

}

다른 수퍼를 추가합니다.

이렇게하면 indexPath를 얻을 수 있으며 indexPath.row 또는 indexPath.section을 사용하여 현재 행을 가져올 수 있습니다.

sender.title 속성을 사용하여 제목을 가져올 수 있습니다.

배열에서이 indexPath를 필수 세부 정보로 사용할 수 있습니다.

관련 문제