2015-10-10 18 views
5

Tôi có chế độ xem bảng động và ô mẫu hiển thị mảng. Câu hỏi của tôi là làm thế nào tôi sẽ thêm một nút hiển thị tên khác nhau trên mỗi tế bào bên trái của tế bào và sau đó là một nhãn ở phía bên phải hiển thị thông tin mảng. Cảm ơn! : DThêm nhãn và nút văn bản vào ô xem bảng động theo lập trình với Swift

Hãy tưởng tượng đây là các tế bào dưới đây:

Bên trái: Nút (info mảng) < -------------------> Bên phải: TextLabel (info mảng)

Trả lời

6

bạn có thể thực hiện như thế này

let titleArray = ["a", "b", "c"] 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 50 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //return no.of cell do you want 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cellHeight = tableView(self.tableView, heightForRowAtIndexPath: NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)) 
    var cell = CustomCell(frame: CGRectMake(0, 0, self.view.frame.width, cellHeight), title: titleArray[indexPath.row]) 
    cell.cellLabel.text = //labelText 
    cell.cellButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside) 

    return cell 
} 

class CustomCell: UITableViewCell { 
    var cellButton: UIButton! 
    var cellLabel: UILabel! 

    init(frame: CGRect, title: String) { 
     super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") 

     cellLabel= UILabel(frame: CGRectMake(self.frame.width - 100, 10, 100.0, 40)) 
     cellLabel.textColor = UIColor.blackColor() 
     cellLabel.font = //set font here 

     cellButton = UIButton(frame: CGRectMake(5, 5, 50, 30)) 
     cellButton.setTitle(title, forState: UIControlState.Normal) 

     addSubview(cellLabel) 
     addSubview(cellButton) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
    } 
} 
+0

Trợ giúp tuyệt vời, cảm ơn bạn :) –

1

tôi có giải pháp cho câu hỏi của bạn, hãy làm theo các bước dưới đây

var data = ["iPad", "iPhone", "iWatch", "iPod", "iMac"] 
var buttonData = ["US","China","London","Canada","Japan"]; 


//UITableViewDataSource Methods 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return 80 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return data.count 
} 

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    var label = UILabel(frame: CGRectMake(280.0, 14.0, 100.0, 30.0)) 
    label.text = data[indexPath.row] 
    label.tag = indexPath.row 
    cell.contentView.addSubview(label) 


    let btn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
    btn.backgroundColor = UIColor.greenColor() 
    btn.setTitle(buttonData[indexPath.row], forState: UIControlState.Normal) 
    btn.frame = CGRectMake(0, 5, 80, 40) 
    btn.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside) 
    btn.tag = indexPath.row 
    cell.contentView.addSubview(btn) 

    return cell 
} 

//Button Action is 
func buttonPressed(sender:UIButton!) 
{ 
    let buttonRow = sender.tag 
    println("button is Pressed") 
    println("Clicked Button Row is",buttonRow) 
} 
+0

Nó nói buttonWithType là không có sẵn, sử dụng xây dựng đối tượng UIButton (type:) ... Bất kỳ đề xuất? –

+0

Hoạt động ngay bây giờ, tuyệt vời. Đã phải cập nhật nó lên cú pháp iOS9! :) –

+0

Vì vậy, làm cách nào để tôi có các nhãn và nút phù hợp với các màn hình kích thước khác nhau? –

Các vấn đề liên quan