tableview页脚中的按钮未被调用

时间:2015-07-23 05:33:01

标签: ios objective-c

我已在UIView

中声明UIButtonUIView
-(void)viewWillAppear:(BOOL)animated
{
    footerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,300)];

    UILabel *totallabel = [[UILabel alloc]initWithFrame:CGRectMake(50,5,320,40)];
    totallabel.text = @"Grand Total =";
    totallabel.font = [UIFont fontWithName:@"Lato-Regular" size:10];
    totallabel.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:totallabel];
    UILabel *lblRs = [[UILabel alloc]initWithFrame:CGRectMake(160,5,320,40)];
    lblRs.text = @"Rs.";
    lblRs.font = [UIFont fontWithName:@"Lato-Regular" size:10];
    lblRs.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:lblRs];
    totalcostlabel = [[UILabel alloc]initWithFrame:CGRectMake(190,5,320,40)];
    totalcostlabel.font = [UIFont fontWithName:@"Lato-Regular" size:10];
    totalcostlabel.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:totalcostlabel];
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    aButton.frame =  CGRectMake(70,50,150,40);
    aButton.backgroundColor = [UIColor colorWithRed:119.0/225.0 green:49.0/255.0 blue:88.0/255.0 alpha:1.0];
    [aButton setTitle:@"Checkout" forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    aButton.titleLabel.font = [UIFont fontWithName:@"Lato-Regular" size:10];
    [aButton addTarget:self action:@selector(btnChackOut:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:aButton ];

    [self.tblView setTableFooterView:footerView];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return footerView;
}

- (void)btnChackOut:(UIButton *)sender {
    NSLog(@"btn clicked");
}

当我单击在我的页脚中声明的Checkout按钮时,不会执行click事件并且不会调用action方法

4 个答案:

答案 0 :(得分:1)

无法调用,因为您没有为页脚设置高度,只需要设置这样的设置页脚高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 300;
}

答案 1 :(得分:1)

我得到了解决方案,现在已经工作了。应用以下代码。

in .h

 #import "CustomCell.h" 

-(IBAction)actionContinue:(id)sender;

@property (strong, nonatomic) IBOutlet UITableView *tblViewCart;

in .m

@synthesize tblViewCart;

- (void)viewDidLoad 
 {
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
   arrayImageCart = [[NSMutableArray alloc]initWithObjects:@"ios7.jpg",@"iphone_6.jpg",nil];
   arrayPrice = [[NSMutableArray alloc]initWithObjects:@"Rs 60,000",@"Rs 55,000",nil];
   arrayDescription = [[NSMutableArray alloc]initWithObjects:@"Made In China",@"Headquarter is in US",nil];
   arrayOtherDetail = [[NSMutableArray alloc]initWithObjects:@"Black Color",@"White Color",nil];
   arraySubTotal = [[NSMutableArray alloc]initWithObjects:@"Rs 60,000",@"Rs 55,000",nil];
   arrayTotal = [[NSMutableArray alloc]initWithObjects:@"110000",nil];
   arrayTotalCharges = [[NSMutableArray alloc]initWithObjects:@"2000",nil];
   arrayYouPay = [[NSMutableArray alloc]initWithObjects:@"112000", nil];
   self.tblViewCart.separatorColor = [UIColor clearColor];
 }

// Button Action
-(IBAction)actionContinue:(id)sender
 {


 }

#pragma mark - UITableView Delegate Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 203;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0)
       return arrayPrice.count;
    else
       return arrayYouPay.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"Reuse"];
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
    if(indexPath.section==0)
    {
      cell = [nib objectAtIndex:0];
      cell.imageViewCart.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImageCart objectAtIndex:indexPath.row]]];
      cell.lblPriceDetails.text = [NSString stringWithFormat:@"%@",[arrayPrice objectAtIndex:indexPath.row]];
      cell.lblDescriptionAbtProduct.text = [NSString stringWithFormat:@"%@",[arrayDescription objectAtIndex:indexPath.row]];
      cell.labelOtherDetails.text = [NSString stringWithFormat:@"%@",[arrayOtherDetail objectAtIndex:indexPath.row]];
      cell.lblSubTotal.text = [NSString stringWithFormat:@"%@",[arraySubTotal objectAtIndex:indexPath.row]];
   }
   else
   {
      cell = [nib objectAtIndex:1];
      cell.lblTotal.text = [NSString stringWithFormat:@"%@",[arrayTotal objectAtIndex:indexPath.row]];
      cell.lblTotalCharges.text = [NSString stringWithFormat:@"%@",[arrayTotalCharges objectAtIndex:indexPath.row]];
      cell.lblYouPay.text = [NSString stringWithFormat:@"%@",[arrayYouPay objectAtIndex:indexPath.row]];
   }
   return cell;
}

通过NewFile创建自定义单元格> Source-> CocoaTouchClass-> Next> SubClassof:UITableViewCell,Class->带CheckBox的CustomClass还创建XIB文件(这是有价格详细信息)。 然后在CustomCell.xib文件内创建另一个UITableViewCell,并为Total Amount和Pay赋予CustomCell名称。

答案 2 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 304, 40)];
    footerView.backgroundColor = [UIColor whiteColor];

    UIButton *add_member=[UIButton buttonWithType:UIButtonTypeCustom];
    add_member.layer.borderWidth = 0.8;
    add_member.layer.cornerRadius = 10;
    add_member.layer.masksToBounds = YES;
    add_member.layer.borderColor =[[UIColor lightGrayColor] CGColor];
    add_member.layer.shadowColor = [[UIColor whiteColor] CGColor];
    add_member.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    add_member.layer.shadowOpacity = 0.0;
    [add_member setTitle:@"Add Member" forState:UIControlStateNormal];
    add_member.titleLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:16];
    [add_member addTarget:self action:@selector(AddNewMember:) forControlEvents:UIControlEventTouchUpInside];
    [add_member setTitleColor:[UIColor colorWithRed:140.0/255.0 green:198.0/255.0 blue:63.0/255.0 alpha:1.0] forState:UIControlStateNormal];//set the color this is may be different for iOS 7
    add_member.frame=CGRectMake(10, 5, 284, 30); //set some large width to ur title

    [footerView addSubview:add_member];
    return footerView;
}
#pragma mark - Add member

-(IBAction)AddNewMember:(id)sender
{
      //Some code
}

答案 3 :(得分:0)

制作按钮的属性,并在动作块中提及属性名称,如下所示。

-(IBAction)btn_check:(id)sender
{
   BOOL buttonstate;//default state is false.
    if(!buttonstate)
    {
      //Declare your action code here.
           //Use the button property name here for your action.
    }
   else
   {
      //Declare the button release code here ,its not mandatory.
   }