2012-07-11 101 views
3

我有一個名爲NewCell的自定義單元格的UITableView,我試圖在單擊事件後在NewCell中顯示一個名爲'selectedView'的UIView。iOS UITableView單元格選擇

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NewCell *cell = (NewCell*)[tableView cellForRowAtIndexPath:indexPath]; 
    [cell.selectedView setHidden:NO]; 
} 

編輯 - 添加單元創建代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"NewCell"; 
    NewCell *productCell = (NewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (productCell == nil) 
    { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) 
     { 
      if([currentObject isKindOfClass:[UITableViewCell class]]) 
      { 
       productCell = (NewCell *) currentObject; 
       break; 
      } 
     } 
    } 

    Product *mainProduct = [self.productsArray objectAtIndex:indexPath.row]; 

    float floatPrice = [mainProduct.price floatValue]; 
    NSString *priceFormat; 
    if ((int)floatPrice == floatPrice) priceFormat = @"$%.0f"; else priceFormat = @"$%.2f"; 

    produtCell.productPrice.text = [NSString stringWithFormat:priceFormat, floatPrice]; 

    return productCell; 
} 

一切工作正常,並表填充,但是當我選擇一個單元格,它不會顯示在這是在表中選定的單元格selectedView ,而是它在對面的單元格中顯示selectedView。 我的意思是,如果第0行和第1行顯示在屏幕上,並且我選擇第1行,則會顯示第0行上的selectedView,如果我選擇第0行,則會顯示第1行上的selectedView。

很奇怪,我似乎無法告訴它爲什麼這樣做。

我一定在做錯事。

+0

這看起來好像沒什麼問題。你能分享你的單元格創建代碼嗎? – mopsled 2012-07-11 15:24:44

+0

我添加了代碼 – boostedz06 2012-07-11 16:25:19

+0

darn,我想通了。我仍然確實爲表格設置了productCell.selectionStyle。我刪除了,現在它工作。但是,隨着這一點,它使我有一個尷尬的副作用,我在OP – boostedz06 2012-07-11 16:42:40

回答

5

它這樣做的原因是因爲你需要設置你選擇的風格沒有像這樣:

productCell.selectionStyle = UITableViewCellSelectionStyleNone; 
相關問題