将IBAction添加到自定义UItableviewCell中的按钮

时间:2012-02-02 13:35:10

标签: objective-c uitableview ibaction

所以我想订购不同的产品。在这 我有一个自定义的uitableview单元格,上面有一个按钮。用我的IBAction给NSMutualArray。这是我的IBAction。我在我的viewdidload中分配了我的arrayBestelling。

-(IBAction)addProduct:(NSMutableArray *) arrProduct{
    [arrayBestelling addObject:arrProduct];

}

好的,这是我的整个tableviewMethod。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    productTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"productTableviewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {   
                cell = (productTableviewCell*)view;
            }
            }
    }
    if(searching) {
        cell.proId.text = [[[copyArrayProducts valueForKey:@"Pro_id"] objectAtIndex:indexPath.row] objectAtIndex:0];
        cell.product.text = [[[copyArrayProducts valueForKey:@"Pro_naam"] objectAtIndex:indexPath.row] objectAtIndex:0];
        cell.prijs.text = [[[copyArrayProducts valueForKey:@"Pro_prijs"] objectAtIndex:indexPath.row] objectAtIndex:0];

    }else {


    NSString *strId = [[[arrayProducts valueForKey:@"Pro_id"] objectAtIndex:indexPath.row] objectAtIndex:0];
    NSString *strNaam = [[[arrayProducts valueForKey:@"Pro_naam"] objectAtIndex:indexPath.row] objectAtIndex:0];
    cell.product.text = strNaam;
    cell.proId.text = strId;
    NSMutableString *strPrijs = [NSMutableString stringWithString: @"€ "];
    [strPrijs appendString:[[[arrayProducts valueForKey:@"Pro_prijs"] objectAtIndex:indexPath.row] objectAtIndex:0]];
    cell.prijs.text = strPrijs;
    NSString *strAantal = cell.aantal.text;

    arrayProBestelling = [[NSMutableArray alloc] init];
        [arrayProBestelling addObject:strId];
        [arrayProBestelling addObject:strNaam];
        [arrayProBestelling addObject:strPrijs];
        [arrayProBestelling addObject:strAantal];

    }

   **[cell.btnadd addTarget:self action:@selector(addProduct:arrayProBestelling) forControlEvents:UIControlEventTouchUpInside]**

    return cell;

}

有人可以帮忙吗?

编辑!

2 个答案:

答案 0 :(得分:0)

我建议添加名为Product的自定义数据类,并将这些名称,价格等作为字段添加。然后将Products添加到MutableArray。

-(IBAction)addProduct:(Product *) product{
    [productArray addObject:product];
}

然后,您可以使用SortDescriptors对数组进行排序: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/SortDescriptors/Concepts/Creating.html

答案 1 :(得分:0)

您可以为单元格的-accessoryView添加按钮。如果您正在构建自己的自定义单元格,这一点都不难。但是对于一个简单的简单实现,只需创建一个UIButton,分配一个目标,然后将其添加到accessoryView。

UIImage *image = [UIImage imageNamed:@"yourbutton.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];

[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;