2017-07-06 172 views
0

有人可以告诉如何将UIButton添加到单元行的末尾吗?我有一个包含10行的单元格,我想通过编程方式在单元格的末尾创建一个按钮。如何将一个按钮添加到单元格末尾Swift

+0

你是什么意思A CEL l 10行?一个单元应该是一个行。你的意思是你有十个电池吗? –

+0

让我们只是说我想在单元格的末尾添加一个按钮。这就是全部 – Jack

+1

@Jack,如果按单元格表示单元格,那么你可以创建一个自定义原型单元格,并在那里放置一个按钮并使用约束来对齐。但是你的问题很模糊,所以我建议你指定它。 – Olter

回答

2

我以为你知道在UITableView上创建原型单元格。根据上面的问题,在现有的tableView上还需要一个原型单元格。在下面的代码中,UITableView有两个prototypeCell

在UIStoryBoard: enter image description here

尝试下面的代码

import UIKit 
class TableViewController: UIViewController,UITableViewDataSource { 


@IBOutlet var tableView: UITableView! 

var greetings = ["Vanakkam","Hello","Hi","Ni Hao","Namastae","Selamat Tinga","Oi"] 
override func viewDidLoad(){ 
    super.viewDidLoad() 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return greetings.count+1 // +1 for button 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


    if indexPath.row < greetings.count{ // (0...N).count = N+1. Already last row added at numberOfRowsInSection 
     let normalCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") 
     normalCell?.textLabel?.text = greetings[indexPath.row] // 
     normalCell?.textLabel?.textAlignment = .center 
     return normalCell! 
    }else{ // last row 
     let buttonCell = self.tableView.dequeueReusableCell(withIdentifier: "cellButton") 
     return buttonCell! 
    } 
    } 
} 

输出:

enter image description here

相关问题