hinzufügen-Schaltfläche in der Fußzeile tableview in swift
Möchte ich hinzufügen, dass ein button in einer Tabelle eine Fußzeile. Ich habe geschrieben, dieser code aber der button funktioniert nicht.
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
var footerView : UIView?
footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))
footerView?.backgroundColor = UIColor.blackColor()
let dunamicButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
dunamicButton.backgroundColor = UIColor.greenColor()
dunamicButton.setTitle("Button", forState: UIControlState.Normal)
dunamicButton.frame = CGRectMake(0, 0, 100, 50)
dunamicButton.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
footerView?.addSubview(dunamicButton)
return footerView
}
func buttonTouched(sender:UIButton!){
println("diklik")
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50.0
}
Dank!
Bearbeiten meiner Klasse tableView
import UIKit
import GoogleMobileAds
Klasse MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, SetingTableViewControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var tabelView: UITableView!
var data = ["BINATANG","BUAH","MAKANAN","SAYURAN","TUBUH","RUMAH","SEKOLAH","WARNA","ALAM","KENDARAAN","MAINAN"]
override func viewDidLoad() {
super.viewDidLoad()
self.tabelView.rowHeight = 100.0
}
func SetingTableViewControllerDidCancel(controller : SetingTableViewController)
{
dismissViewControllerAnimated(true, completion: nil)
}
func SetingTableViewControllerDidDone(controller : SetingTableViewController){
dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tabelView.dequeueReusableCellWithIdentifier("cell")
as! KategoriCell
cell.imgCell.image = UIImage(named: data1[indexPath.row]+"2")
cell.labelCell.text = data[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "playId"{
let indexPath = tabelView.indexPathForSelectedRow()
let item = data[indexPath!.row]
let detailViewController = segue.destinationViewController as! PlayViewController
detailViewController.kategoriKe = indexPath!.row
}
else if segue.identifier == "settingId"{
let navigationController = segue.destinationViewController
as! UINavigationController
let controller = navigationController.topViewController
as! SetingTableViewController
controller.delegate = self
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
NSUserDefaults.standardUserDefaults().setInteger(indexPath.row, forKey: "kategoriIdx")
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var height : CGFloat
height = 50
return height
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
var footerView : UIView?
footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))
footerView?.backgroundColor = UIColor.blackColor()
let dunamicButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
dunamicButton.backgroundColor = UIColor.greenColor()
dunamicButton.setTitle("Button", forState: UIControlState.Normal)
dunamicButton.frame = CGRectMake(0, 0, 100, 50)
dunamicButton.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
footerView?.addSubview(dunamicButton)
return footerView
}
func buttonTouched(sender:UIButton!){
println("diklik")
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50.0
}
func navigationController(navigationController: UINavigationController,
willShowViewController viewController: UIViewController,
animated: Bool) {
if viewController === self {
NSUserDefaults.standardUserDefaults().setInteger(
-1, forKey: "kategoriIdx")
}
}
}
Zu keinem Ergebnis kam, oder nicht?
Taste nicht funktioniert....keine respons.
Versuchen Sie, diese - dunamicButton.addTarget(self, action: Selector("buttonTouched:"), forControlEvents: UIControlEvents.TouchUpInside)
Taste nicht funktioniert....keine respons.
Versuchen Sie, diese - dunamicButton.addTarget(self, action: Selector("buttonTouched:"), forControlEvents: UIControlEvents.TouchUpInside)
InformationsquelleAutor Zahed Ri | 2015-07-04
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich habe deinen code getestet und es ist in Ordnung, aber wenn es nicht für Sie arbeitet, dann gibt es eine weitere Möglichkeit, es zu tun, gehen Sie zu Ihrem ViewController im storyBoard, dann fügen Sie ein neues view-Schlag Ihr Handy, wie unten gezeigt:
Danach können Sie verbinden eine Aktion, dass button mit diesem code:
Überprüfen DIESE Beispiel-code.
Dies wird gut funktionieren.
HIER ist Beispiel-Projekt mit dem code.
können Sie mir Beispiel-Projekt?
mein code oben..
Kheni Dank dein code funktioniert .. nur das problem ist UIView ist die Anzeige auf der Ab, wenn ich versuche zu Laden, der Verwandte UItableview
Können Sie Ihre demo-Projekt?
InformationsquelleAutor Dharmesh
Ändern Sie einfach Ihre Funktion Aktion :
InformationsquelleAutor Ashish Kakkad