2011-06-09 26 views
0

我设计了一个自定义表格单元格。其中显示产品信息。定制设计的UITableViewCell中的CheckBox项目,iPhone SDK

当我实现CellForRowAtIndexPath我这样做。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *sectionTableCellIdentifier = [[NSString alloc] initWithFormat:@"GLItemTableCellIdentifierNumber%d",indexPath.section]; 
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GLItemDetailsTableCellIdentifier"]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier]; 


    if (cell == nil) 
    { 


     NSDictionary *dict = [self.listData objectAtIndex:indexPath.row]; 
     ItemsListTableCell *cell = (ItemsListTableCell *)[tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier];    
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemsListTableCell" 
                owner:self options:nil]; 
     for (id oneObject in nib) 
     { 
      if ([oneObject isKindOfClass:[ItemsListTableCell class]]) 
      { 
       cell = (ItemsListTableCell *)oneObject; 
      } 
     } 

     NSString *priceinfo = [[NSString alloc] initWithFormat:@"$%@",[dict objectForKey:@"CurrentPrice"]]; 
     NSString *sizeinfo = [[NSString alloc] initWithFormat:@"Size: %@",[dict objectForKey:@"Size"]]; 

     NSString *upcInfo = [[NSString alloc] initWithFormat:@"UPC: %@",[dict objectForKey:@"ID"]]; 
     NSString *strQuantity = [[NSString alloc] initWithFormat:@"%@",[dict objectForKey:@"Quantity"]]; 

     cell.lblProductName.text = [dict objectForKey:@"Name"]; 
     cell.lblSize.text = sizeinfo; 
     cell.lblBrand.text = [dict objectForKey:@"BrandName"]; 
     cell.lblProductCode.text = upcInfo;   
     cell.lblQuantity.text = strQuantity;   
     cell.lblPrice.text = priceinfo; 
     cell.lblStoreName.text = [dict objectForKey:@"StoreName"]; 
     cell.isSelected = NO; 
     [cell.btnSelected addTarget:self action:@selector(cellButtonTapped:) 
     forControlEvents:UIControlEventTouchUpInside]; 

     [upcInfo release]; 
     [priceinfo release]; 
     [strQuantity release]; 
     [sizeinfo release]; 
     return cell; 
    } 
    return cell; 
} 

现在单击事件我做

- (IBAction)cellButtonTapped:(id)sender 
{ 
    UIView *contentView = [sender superview]; 
    ItemsListTableCell *cell = (ItemsListTableCell *)[contentView superview]; 
    NSIndexPath *indexPath = [table indexPathForCell:cell]; 

    NSUInteger buttonRow = [[self.table 
          indexPathForCell:cell] row]; 
    NSUInteger buttonSection = [[self.table 
          indexPathForCell:cell] section]; 

    NSLog(@"Index Path Row : %d",buttonRow); 
    NSLog(@"Index Path Section : %d",buttonSection); 

    ItemsListTableCell *buttonCell = 
    (ItemsListTableCell *)[table cellForRowAtIndexPath:indexPath]; 

    if (buttonCell.isSelected == YES) 
    { 
     buttonCell.isSelected = NO; 
     UIImage *image = [[UIImage imageNamed:@"checkbox-empty.png"] autorelease]; 
     [buttonCell.btnSelected setImage:image forState:UIControlStateNormal]; 
    } 
    else 
    { 
     buttonCell.isSelected = YES; 
     UIImage *image = [[UIImage imageNamed:@"checkbox-full.png"] autorelease]; 
     [buttonCell.btnSelected setImage:image forState:UIControlStateNormal]; 
    } 

    self.txtQuantity.text = buttonCell.lblQuantity.text; 
    NSString *buttonTitle = buttonCell.lblProductName.text; 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"You tapped the button" 
          message:[NSString stringWithFormat: 
            @"You tapped the button for %@", buttonTitle] 
          delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

问题是,当我在复选按钮,点击它会事件。但我无法检测到什么是父单元。因为单元格中有一些值。

回答

1

而不是创建这样的事件(IBAction为),你会如果你想要自己的风格勾选做所有这些在

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

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

if (selectedCell.accessoryType == UITableViewCellAccessoryNone) 
{ 
    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
    if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     selectedCell.accessoryType = UITableViewCellAccessoryNone; 
    } 

} 

,你可以在这里设置起来,并玩完的事情。使东西更容易!

+0

其实我不需要选择整行。因为整行有一些其他事件,例如价格或数量更新。我用accessorytype测试了这个解决方案,它可以用于案例或行选择。但我的要求是点击指定区域的细胞,因为我的细胞也很大。 – 2011-06-09 16:38:00

+0

哦,哦! gotcha! – Legolas 2011-06-09 16:39:55

相关问题