2015-09-20 86 views
0

我正在研究一個原型(我在Objective C上太綠了),該原型具有兩個不同定製原型單元的tableview,每個原型單元都有自己的一組組件,佈局和高度。預期的行爲是,選中時,行將展開並顯示單元格類型b,並且之前選定的行將返回到前一個高度和單元格類型。Objective C:在取消選擇單元格時切換UITableViewCell

好吧,所以我遇到的問題是我無法獲得以前選擇的行來更改它的單元格類型,高度工作正常,所以我最終顯示的是擴展單元格的較短版本,但沒有所需單元格類型的組件和佈局,如果我滾動瀏覽tableview並返回到之前選定行的位置,它將顯示正確的單元格類型,就像它沒有刷新其餘行一樣。

這是我對的tableView的cellForRowAtIndexPath代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier; 

    UITableViewCell *cell = nil; 

    if(indexPath.row == self.selectedIndex){ 

     CellIdentifier = @"ExpandedCell"; 
     CollapsedCell *collapsedCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     cell = collapsedCell; 

    }else{ 

     CellIdentifier = @"LocationCell"; 
     LocationCell *locationCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     locationCell.transparentBorder.layer.borderWidth = 5.0f; 
     locationCell.transparentBorder.layer.borderColor = [UIColor colorWithRed:255/255 green:255/255 blue:255/255 alpha:0.7].CGColor; 
     cell = locationCell; 

    } 

    BHPlace *currentPlace = self.data[indexPath.row]; 

    NSString *name = currentPlace.name; 

    LocationCell *locationCell = (LocationCell*)cell; 
    locationCell.nameLabel.text = name; 

    cell.backgroundColor = [UIColor clearColor]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    return cell; 
} 

,這裏是我的didSelectRowAtIndexPath方法的代碼:我敢肯定,這是一個簡單的問題

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

    [self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0] animated:YES]; 

    [self.tableView beginUpdates]; 
    self.selectedIndex = (int)indexPath.row; 
     NSArray *paths = @[indexPath]; 
     [self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade]; 

     [self.tableView endUpdates]; 
    [self goToMarker:self.data[indexPath.row]]; 
} 

像往常一樣,這裏面的東西我不越來越正確,但無法找到正確的方法,

謝謝大家爲你的時間

+1

您之前選擇的行添加到'paths'陣列和重新加載這兩個單元格 – Paulw11

+0

我的上帝,不能相信它是如此簡單!如果您將其作爲單獨答案發布,我可以將您的答案標記爲已接受的答案,謝謝! –

回答

1

你只需要重新加載這兩個先前選擇和新選擇行 -

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

    NSMutableArray *paths=[NSMutableArray new]; 

    if (self.selectedIndexPath != nil) { 
     [paths addObject:self.selectedIndexPath]; 
     [tableView deselectRowAtIndexPath:self.selectedIndexPath animated:YES]; 
    } 

    [self.tableView beginUpdates]; 
    self.selectedIndexPath = indexPath; 
    [paths addObject:indexPath]; 
    [self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 
    [self goToMarker:self.data[indexPath.row]]; 
} 

請注意,我改變了你的int財產的NSIndexPath *self.selectedIndexPath

+0

謝謝,我也相信最好將int屬性切換到NSIndexPath,所以我會這麼做。 –

相關問題