【问题标题】:How to give IBAction event to custom cell's UIButton?如何将 IBAction 事件赋予自定义单元格的 UIButton?
【发布时间】:2012-02-06 05:35:52
【问题描述】:

我想给自定义单元格的 UIButton IBAction 事件。

我尝试使用以下代码,但它崩溃了。

如何创建委托并在我的 UITableView 类中使用它?

Customcell class

-(IBAction)btnShowAttachmentClick:(UITableView *)aTblViewCommon
{
    [(ShowTechAndProfDetailsVC *)aTblViewCommon.delegate performSelector:@selector(showAttachmentsFromCell:) withObject:nil];
}

UIViewController class having UITableView

-(void)showAttachmentsFromCell:(UIButton *)sender
{
    gotoClass *obj = [[gotoClass alloc] initWithNibName:@"gotoClass" bundle:nil];
    [self.navigationController pushViewController:obj animated:YES];
    [obj release];
}

这里是cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    if(appDelegate.intCountSwipeForView == 1 || appDelegate.intCountSwipeForView == 2)
    {
        objCellView = [ViewExpAndEduCustomCell dequeOrCreateInTable:tableView];

        objCellView.selectionStyle = UITableViewCellSelectionStyleNone;
        //objCellView.btnPaperClip.tag = indexPath.row;
        [objCellView.btnPaperClip addTarget:self action:@selector(showAttachmentsFromCell:) forControlEvents:UIControlEventTouchUpInside];


        objCellView.lblName.text = @"asdfsdf";
        objCellView.lblTitleOrCourse.text = @"asdf";
        objCellView.lblStartDate.text = @"23-Nov-2001";
        objCellView.lblEndDate.text = @"04-Feb-2002";


        [objCellView.txtViewSummary setTextColor:TEXT_COLOR_GRAY_FOR_TABLECELL];
        [objCellView.txtViewSummary setFont:[UIFont systemFontOfSize:17.0]];
        [objCellView.txtViewSummary setContentInset:UIEdgeInsetsMake(-5, 0, 0, 0)];
        [objCellView.txtViewSummary setBackgroundColor:[UIColor clearColor]];
        [objCellView.txtViewSummary setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    }
if(appDelegate.intCountSwipeForView == 1){
        objCellView.txtViewSummary.text = @"Experience.This is sample text.This text can be of 150 characters.No more than that.You can also attach your achievment but only pdf,Image or video";
        return objCellView;
}

【问题讨论】:

  • 在tableview中显示你调用方法的代码
  • 我不是在tableview中调用,我只是在tableviewcell类的performselector中调用
  • 你用 UIButton 的 TableView 的辅助视图试过了吗?
  • 我正在使用自定义单元格,所以在 UITableViewCell 的 IB 中使用了 UIButton
  • @Roshni 您的代码看起来不错,只需检查按钮和自定义类之间的连接是否正确。

标签: objective-c ios4 uitableview custom-cell


【解决方案1】:

嘿,在 Customcell 类中有一个 IBOutlet of you 按钮,然后在

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //creating cell or resusing cell

    //after getting the cell and casting it into your Customcell
        [cell.mButton addTarget:self action:@selector(showAttachmentsFromCell:) forControlEvents:UIControlEventTouchUpInside];

}

【讨论】:

  • 它没有进入选择器方法
  • 我已经为 UITableViewCell 类中的那个按钮使用了 IBOutlet。并在 UITableView 的类中调用了方法
  • 我想我需要创建委托
  • @roshini 你在 objCellView 中为 btnPaperClip 创建了属性并合成了它..?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
相关资源
最近更新 更多