簡體   English   中英

如何在TableView的每一行中添加按鈕,圖像和其他UI元素? (iOS,快速)

[英]How to add buttons, images and other UI elements in each row of TableView? (iOS,Swift)

我可以使用Swift在iOS的UITableView中顯示文本列表(每行一條文本)。 我的問題是-如果我想在每行中添加更多UI元素(例如按鈕,圖像等),該怎么辦? 希望能為Swift提供幫助。

我已經能夠做到:

我使用的代碼

class ViewController: UIViewController, UITableViewDelegate {

var cellInfo = ["Rob", "Timmy", "George"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}


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

    return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell") //my cell is the identifier

    cell.textLabel?.text = cellInfo[indexPath.row] //"Test"


    return cell

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

結果我有(下)。 我只能添加文本,如何添加更多元素? 在此處輸入圖片說明

從main.storyboard中,將imageView,標簽和所需的任何內容拖到原型單元中。

您還需要為拖動到其中的每個元素建立IBOutlets,以便可以在代碼中引用它們。

還有其他方法,例如選擇單元格樣式。 例如,表格具有4種樣式。 基本樣式具有imageView和標簽。

如果需要更多功能,則應自定義樣式,然后將所需的任何內容拖到原型單元中。

如果樣式為“默認”,則應該只看到textLabel的文本。 如果單元格的樣式為“自定義”,則可以添加更多元素。 然后您可以在Interface Builder中添加元素並建立出口或以編程方式將其添加到單元中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM