2017-06-06 113 views

回答

2

Apple's Docs您必须实现heightForFooterInSection方法,否则您的viewForFooterInSection将不会执行任何操作。

斯威夫特3

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return 100 
} 

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 

guard section == 0 else { return nil } // Can remove if want button for all sections 

let footerView = UIView(frame: CGRect(0, 0, 320, 40)) 
     let myButton = UIButton(type: .custom) 

     myButton.setTitle("My Button", for: .normal) 
     myButton.addTarget(self, action: #selector(myAction(_:)), for: .touchUpInside) 

     myButton.setTitleColor(UIColor.black, for: .normal) //set the color this is may be different for iOS 7 
     myButton.frame = CGRect(0, 0, 130, 30) //set some large width to ur title 
     footerView.addSubview(myButton) 
     return footerView; 

} 

func myAction(_ sender : AnyObject) { 

} 

Objective-C的

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    if(section == 0) //Decide which footer according to your logic 
    { 
     UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; 
     UIButton *myButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [myButton setTitle:@"Add to other" forState:UIControlStateNormal]; 
     [myButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//set the color this is may be different for iOS 7 
     myButton.frame=CGRectMake(0, 0, 130, 30); //set some large width to ur title 
     [footerView addSubview: myButton]; 
     return footerView; 
    } 
} 

- (void)myAction:(id)sender 
{ 
     NSLog(@"add to charity"); 
} 
+0

FWIW我觉得你heighForFooterInSection和footerView框架的高度...的heightForFooterView将aupercede,让你的身高是100间不是40 – Honey

+0

谢谢你的答案! –

相关问题