0

我有一个视图控制器
名称:FirstVC

我在那个视图控制器中有表格视图。在我的表格视图中,我加载了一个自定义单元格。在我的自定义单元格中有 1 个标签和 1 个按钮。我从 FirstVC 设置标签以及自定义单元格的按钮。请参见下面的代码。

在 FirstVC.h 中

@interface FirstVC : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UITableView *TblView;
    IBOutlet CustomCell *customCell;
}
- (void)addCounter:(NSInteger)pCat_ID;

@end

在 FirstVC.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *cellIdentifier = @"Cell";
        CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil){
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPhone" owner:self options:nil];
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell = categoryCustomCell;
        }
NSInteger pCat_id = [[[arrayCategoryData objectAtIndex:indexPath.row] objectForKey:@"cat_id"] integerValue];
        [customCell setTextForLable:@"Test"];
        [customCell setAddCounterButton:pCat_id];

        return cell;
}

- (void)addCounter:(NSInteger)pCat_ID
{
    //Some code here….
}

在 CustomCell.h

@interface CustomCell : UITableViewCell
{
    IBOutlet UILabel *lblCategoryName;
    IBOutlet UIButton *btnAddCounter;
}

- (void)setTextForLable:(NSString *)cat_name;
- (void)setAddCounterButton:(NSInteger)cat_ID;

@end

在 CustomCell.m

@implementation CustomCell

- (void)setTextForLable:(NSString *)cat_name
{
    lblCategoryName.text = cat_name;
}

- (void)setAddCounterButton:(NSInteger)cat_ID
{
    [btnAddCounter addTarget:self action:@selector(addCounter:cat_ID) forControlEvents:UIControlEventTouchUpInside];
}

@end

我想从自定义单元格调用addCounter:(NSInteger)pCat_ID方法。但这个想法行不通。请建议我新的想法。

4

1 回答 1

3
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"Cell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPhone" owner:self options:nil];
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell = categoryCustomCell;
    } 
     NSInteger pCat_id = [[[arrayCategoryData objectAtIndex:indexPath.row] 
    // call your method from here.. and put your method in this file then it will work for you
 [cell.btnAddCounter addTarget:self action:@selector(addCounter:pCat_id) forControlEvents:UIControlEventTouchUpInside];

    return cell;

}

于 2012-11-03T09:00:05.320 回答