2015-02-06 53 views
-1

我正在執行inline date picker in a table view。但是,我正在努力解決'didSelectRowAtIndexPath'問題。'didSelectRowAtIndexPath'檢測到太多的行

的部分和的tableView的行被定義使用NS_ENUM,如下所示:

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 
}; 

然後,我實現「didSelectRowAtIndexPath方法」,這樣,如果「ThirdSectionFirstRow」由用戶選擇,日期選擇器將被顯示。我使用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 2015-02-06 20:50:36

+1

所有枚舉都從零開始,所以它們全部重疊 - 它們只是整數,並且在執行時這些名稱早已消失。但是無論如何你無法按行號唯一標識一個單元格。 – 2015-02-06 20:50:40

+1

我認爲你應該重新考慮你的整個方法。使用這組枚舉來定義段和行不是處理分段表視圖的常用方法。 – rdelmar 2015-02-06 20:50:56

回答

1

基本上你是不是比較indexPath的部分,你只是比較ENUM行改變的條件類似下面的ThirdSectionFirstRow,

- (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]; 
} 

,如果你有太多的條件下更好地使用開關的情況下像

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

Irfan真的很有幫助,謝謝。 – narner 2015-02-06 20:55:05

1

假設你在討論實際的tableview部分,那麼你並沒有測試section-only行,所以你描述的行爲是正確的(也就是說,任何匹配的行都會是你的成功條件如果)。

僞代碼,你需要做這樣的事情:

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

如果你想擁有的第三部分的第一排只有行爲,你應該檢查你的indexPathsection以及。

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

你枚舉是非常誤導;爲什麼你將TotalComponents的值設爲4TotalRows的值爲5?我建議遷移到實際的數據結構。

另外,您已從SectionOneSectionTwo切換爲ThirdSection的倒序排序。爲什麼?

0

您只測試行,如果您有不同的部分,您還必須測試該部分。

1

更簡單一點,每次點擊任意表格行時,都會觸發「didSelectRowAtIndexPath」方法,因此您的邏輯很好,因爲您有一個IF來檢測被點擊的行是否是第三個行。另一方面,「indexPath」的「row」屬性是一個NSInteger,所以也許表達式「(indexPath.row == ThirdSectionFirstRow & & self.datePickerIsShowing == NO)」並沒有給你預期的結果,爲什麼'你改變它爲'(indexPath.row == 2)「,2是第三行。也許這不是強制檢查選擇器是否顯示。