iphone应用程序中自定义单元格的调用方法
我有一个视图控制器iphone应用程序中自定义单元格的调用方法,iphone,uibutton,custom-cell,Iphone,Uibutton,Custom Cell,我有一个视图控制器 姓名:FirstVC 我在那个视图控制器中有表视图。在我的表格视图中,我加载了一个自定义单元格。在我的自定义单元格中有一个标签和一个按钮。我从FirstVC设置自定义单元格的标签和按钮。参见下面的代码 在FirstVC.h中 @interface FirstVC : UIViewController <UITableViewDataSource, UITableViewDelegate> { IBOutlet UITableView *TblView;
姓名:FirstVC 我在那个视图控制器中有表视图。在我的表格视图中,我加载了一个自定义单元格。在我的自定义单元格中有一个标签和一个按钮。我从FirstVC设置自定义单元格的标签和按钮。参见下面的代码 在FirstVC.h中
@interface FirstVC : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UITableView *TblView;
IBOutlet CustomCell *customCell;
}
- (void)addCounter:(NSInteger)pCat_ID;
@end
在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\u ID方法。但这个想法行不通。请给我提个新主意
- (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;
}
}cat\u ID有什么用?它是如何计算的?cat_ID的用途是什么?它是如何计算的呢?根据我的说法是正确的,只需在选择器中传递索引路径,就可以得到单击了哪个按钮的行。根据我的说法,只需在选择器中传递索引路径,就可以得到单击了哪个按钮的行
- (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;