Swift:在每个单元格中添加一个 UIButton,同时在代码中有一个 UITableViewCell 但在情节提要中没有
Swift: Adding a UIButton in each cell while having a UITableViewCell in the code but not in the storyboard
我有一个轨道列表,它代表存储在数组中的任务列表,无论如何,我需要在每个名为 "Track" 的旁边有一个按钮,以便用户可以跟踪到这个任务通过移动到具有此功能的另一个页面。
我的问题是我无法让按钮显示在每个单元格旁边。
在table视图中显示信息的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = trackMgr.tracks[indexPath.row].name
cell.detailTextLabel?.text = String(trackMgr.tracks[indexPath.row].score)
return cell
}
这是我的 table 视图在故事板中的样子:
here
我试图做的是声明 "mybutton" 等于 UIButton!在顶部,然后调用 cell.mybutton.text = "text" 但它没有用。
您可以使用以下代码在每个单元格上创建按钮,相应地您可以在按下按钮后执行跟踪任务。
看看下面的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = "hello"
let track_Button = UIButton()
track_Button.setTitle("Track", forState: .Normal)
track_Button.setTitleColor(UIColor.blueColor(), forState: .Normal)
track_Button.frame = CGRectMake(self.view.frame.size.width-100, 0, 100, 40)
track_Button.backgroundColor = UIColor.grayColor()
track_Button.addTarget(self, action: "track_Button_Pressed:", forControlEvents: .TouchUpInside)
cell.addSubview(track_Button)
return cell
}
func track_Button_Pressed(sender: UIButton!) {
// Track Functionality
println("Add Track Functionality here")
}
我有一个轨道列表,它代表存储在数组中的任务列表,无论如何,我需要在每个名为 "Track" 的旁边有一个按钮,以便用户可以跟踪到这个任务通过移动到具有此功能的另一个页面。 我的问题是我无法让按钮显示在每个单元格旁边。 在table视图中显示信息的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = trackMgr.tracks[indexPath.row].name
cell.detailTextLabel?.text = String(trackMgr.tracks[indexPath.row].score)
return cell
}
这是我的 table 视图在故事板中的样子: here
我试图做的是声明 "mybutton" 等于 UIButton!在顶部,然后调用 cell.mybutton.text = "text" 但它没有用。
您可以使用以下代码在每个单元格上创建按钮,相应地您可以在按下按钮后执行跟踪任务。
看看下面的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = "hello"
let track_Button = UIButton()
track_Button.setTitle("Track", forState: .Normal)
track_Button.setTitleColor(UIColor.blueColor(), forState: .Normal)
track_Button.frame = CGRectMake(self.view.frame.size.width-100, 0, 100, 40)
track_Button.backgroundColor = UIColor.grayColor()
track_Button.addTarget(self, action: "track_Button_Pressed:", forControlEvents: .TouchUpInside)
cell.addSubview(track_Button)
return cell
}
func track_Button_Pressed(sender: UIButton!) {
// Track Functionality
println("Add Track Functionality here")
}