自定义设计的 UITableViewCell 中的 CheckBox 项,iPhone SDK

Posted

技术标签:

【中文标题】自定义设计的 UITableViewCell 中的 CheckBox 项,iPhone SDK【英文标题】:CheckBox Item in Custom Designed UITableViewCell , IPhone SDK 【发布时间】:2011-06-09 14:57:46 【问题描述】:

我设计了一个自定义表格单元格。显示产品信息。

当我实现 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;
    


如果你想要一个自己风格的复选标记,你可以在这里设置它们并完成一些事情。让事情变得更容易!

【讨论】:

其实我不需要选择整行。因为整行还有一些其他事件,例如价格或数量更新。我用附件类型测试了这个解决方案,它适用于案例或行选择。但我的要求是单击单元格的指定区域,因为我的单元格也很大。

以上是关于自定义设计的 UITableViewCell 中的 CheckBox 项,iPhone SDK的主要内容,如果未能解决你的问题,请参考以下文章

UITableViewCell 中的大小类

自定义 UITableViewCell 中的自定义 UIView

自定义 UITableViewCell 中的 UIPickerView

自定义 uitableviewcell 中的 Uibuttons

带有 XIB 的自定义 UITableViewCell

自定义 uitableviewcell 中的 uiscrollview