2012-09-14 39 views
2

我在設置UITableView時發生了一個奇怪的錯誤。發生什麼事是當我第一次點擊它時什麼也沒有發生(它應該改變一個UIView),但是當我點擊上面或下面的一個單元格時,它就會變成我想要顯示的UIView。我創建它就像我創建的每個其他tableview,這裏是代碼:奇怪的UITableView錯誤

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    TableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320.0,120.0)]; 
    // Table View is an iVar in the header file 
    TableView.dataSource = self; 
    TableView.delegate = self; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  { 

    return 44; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

    return 10; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return 5; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

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

    static NSString* CellIdentifier = @"CELLIDENTIFIER"; 
    UITableViewCell* cell = [TableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
     [cell setBackgroundView:[[GMCellBackground alloc] init]]; 
    } 

    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 
+0

The TeachingsCellIdentifier是一個錯字。我遺漏了切換UIViews的代碼,但我確實在 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath –

+0

有同樣的錯誤,我即將發佈的問題,但這是第一個寫這個問題時的建議。 – user2387149

回答

3

我想我看到你的問題。你在你的評論中寫道,你改變看法在

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 

需要注意的是didDeselect沒有didSelect。你想,我覺得是改變

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

意見這是一個常見的錯誤造成的,我認爲人們接受Xcode的自動完成建議沒有注意到,這不是正確的方法。

第二次點擊的原因是第一個被選中的單元格被取消選中,所以你的方法被調用。

+0

啊,那是什麼!愚蠢的自動完成哈哈。謝謝! –

+0

不要忘記將此標記爲正確答案 –

+0

感謝您提醒我 –