2012-07-11 150 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;