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

时间:2011-06-09 14:57:46

标签: iphone cocoa-touch uitableview uibutton

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

当我实现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 个答案:

答案 0 :(得分: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;
    }

}

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