2015-02-06 5 views
-1

inline date picker in a table view을 구현 중입니다. 그러나, 나는 'didSelectRowAtIndexPath'문제로 어려움을 겪고있다.'didSelectRowAtIndexPath'너무 많은 행을 감지했습니다.

typedef NS_ENUM(NSInteger, SectionOne) 
{ 
    SectionOneFirstRow = 0, 
    SectionOneSecondRow, 
    TotalSectionOneRows 
}; 

typedef NS_ENUM(NSInteger, SectionTwo) { 
    SectionTwoFirstRow = 0, 
    SectionTwoSecondRow, 
    SectionTwoThirdRow, 
    SectionTwoFourthRow, 
    TotalSectionTwoRows 
}; 

typedef NS_ENUM(NSInteger, ThirdSection) { 
    ThirdSectionFirstRow = 0, 
    TotalThirdSectionRows 
}; 

typedef NS_ENUM(NSInteger, Section) { 
    SectionOne = 0, 
    SectionTwo, 
    SectionThree, 
    TotalSections, 
    TotalComponents, 
    TotalRows 
}; 

을이어서 'ThirdSectionFirstRow'가 사용자에 의해 선택되면 그래서, 날짜 선택기 것, I '는 didSelectRowAtIndexPath'를 구현한다 아래와 같이

절하고있는 tableView 행은 NS_ENUM를 사용하여 정의 된 표시됩니다. 디버깅을 돕기 위해 NSLog를 사용하고 있습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == ThirdSectionFirstRow && self.datePickerIsShowing == NO){ 
     NSLog(@"Row selected"); 
     [self showDatePickerCell]; 
    } else{ 
     [self hideDatePickerCell]; 
    } 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

는 그러나, 나는 차례의 각 행을 선택하면 내 로깅 결과에 따라,이 메서드를 호출 할 것으로 보인다뿐만 아니라 'ThirdSectionFirstRow가'를 선택하면, 또한 'FirstSectionFirstRow'와 'SecondSectionFirstRow'경우. 아무도 날 논리 내에서 결함을 발견 할 수 있도록이 메서드는 'ThirdSectionFirstRow'가 선택된 경우에만 호출됩니다? 고맙습니다!

+0

정말요? 왜 downvote? – narner

+1

모든 열거 형은 0부터 시작하므로 모두 겹칩니다. 정수일 뿐이며 실행 시간에는 이름이 오래갑니다. 그러나 어쨌든 행 번호별로 셀을 고유하게 식별 할 수는 없습니다. –

+1

나는 당신이 당신의 모든 접근 방식을 다시 생각해야한다고 생각합니다. 이 열거 집합을 사용하여 섹션 및 행을 정의하는 것은 섹션 화 된 테이블 뷰를 처리하는 일반적인 방법이 아닙니다. – rdelmar

답변

1

당신이 indexPath의 섹션을 비교하지 않습니다, 당신은 당신이

같은 스위치 케이스를 사용하는 것이 좋습니다 너무 많은 조건이있는 경우

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == SectionThree && indexPath.row == 0 && self.datePickerIsShowing == NO){ 
     NSLog(@"Row selected"); 
     [self showDatePickerCell]; 
    } else{ 
     [self hideDatePickerCell]; 
    } 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

, ENUM과 행이 ThirdSectionFirstRow을 위해 아래와 같은 조건을 변경 비교하는

switch(indexPath.section) 
{ 
    case SectionOne: 
    if(indexPath.row==0) 
    { 
     [self showDatePickerCell]; 
    } 
    break; 
    case SectionTwo: 
    break; 
} 
+0

정말 도움이되는 Irfan입니다, 감사합니다. – narner

1

실제 테이블 뷰 섹션에 대해 이야기한다고 가정하면 섹션 전용 행에 대한 테스트가 아니므로 설명하는 동작이 정확합니다 (즉, 일치하는 모든 행이 사용자의 성공적인 조건이됩니다). 만약). 세 번째 섹션의 첫 번째 행에 행동 를 원한다면 당신은뿐만 아니라 당신의 indexPathsection을 확인해야

if ((indexPath.section == thirdSection) && (indexPath.row == firstRow) && (any other required conditions)) { 
    // do stuff here 
} 
0

:

의사 코드에서, 당신은 뭔가를해야 할 것 .

귀하의 열거

if (indexPath.section == 2 && indexPath.row == 0) 

은 매우 오해의 소지가있다; TotalComponents4 값을 할당하고 TotalRows 값을 5 값으로 지정하는 이유는 무엇입니까? 실제 데이터 구조로 마이그레이션하는 것이 좋습니다.

또한 SectionOneSectionTwoThirdSection과 반대 순서로 바꿨습니다. 왜? 기본적으로

0

다른 행이있는 경우 행만 테스트하고 섹션도 테스트해야합니다.

1

Narner, "didSelectRowAtIndexPath"메소드는 테이블 행을 탭 할 때마다 실행되므로 논리가 좋으므로 탭핑 된 행이 세 번째 행인 경우 IF를 찾을 수 있습니다. 반면에 "indexPath"의 "row"속성은 NSInteger이므로 "(indexPath.row == ThirdSectionFirstRow & & self.datePickerIsShowing == NO)"는 예상 결과를 제공하지 않습니다. "(indexPath.row == 2)"로 변경하면, 2는 세 번째 행입니다. 피커가 표시되는지 여부를 확인하는 것은 필수적이지 않을 수 있습니다.

관련 문제