2015-04-03 146 views
0

我發現了幾個類似的問題,但沒有回答我的問題。我希望用戶選擇一個表格單元格,打開相機,拍攝照片,並將該照片加載到表格單元格中的imageView中。這是我的代碼到目前爲止的一個片段。我只是失去了如何將這張照片添加到表格單元格中。謝謝!在表格視圖中顯示從相機拍攝的照片

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){ 
    imagePicker.dismissViewControllerAnimated(true, completion: nil) 
    var photo = info[UIImagePickerControllerOriginalImage] as? UIImage 
} 

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

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

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

    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell 

    let row = indexPath.row 
    cell.textLabel?.text = bedroomCells[row] 
    //cell.imageView?.image = 
    return cell 
} 
+0

您需要定義視圖基於表視圖,把一個NSImage中的細胞在列。當您將其出列時,您需要將圖像放置在該單元格中,並將顯示在表格中。 – 2015-04-03 21:58:18

+0

我不確定我是否清楚這一點,但我看到的方式是這樣的。當用戶點擊你的一個tableviewcell時,didSelectRowAtIndexPath被調用,你的相機打開,拍照並存儲在你的數組中。然後你重新加載tableview,它應該從數組中獲取圖像。 – anasimtiaz 2015-04-03 22:01:07

+0

是的,這正是我想要做的。所以最佳做法是將拍攝在相機上的圖像保存到數組中,然後在cellForRowAtIndexPath中檢索該圖像? – Cassandra 2015-04-03 22:30:50

回答

2

嘗試了這一點,似乎是爲我工作:

class ExampleTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    struct TestStruct { 
     var text: String? 
     var image: UIImage? 
    } 

    var imagePicker = UIImagePickerController() 
    var bedroomCells = [TestStruct]() 
    var lastSelectedIndex: NSIndexPath? 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     imagePicker.delegate = self 
     for var i = 0; i < 10 ; i++ { 
      var entry = TestStruct(text: "\(i)", image: nil) 
      bedroomCells.append(entry) 
     } 
    } 

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


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

     let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as UITableViewCell 
     cell.textLabel?.text = bedroomCells[indexPath.row].text 
     cell.imageView?.image = bedroomCells[indexPath.row].image 

     return cell 
    } 


    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     self.lastSelectedIndex = indexPath // Save the selected index 
     self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
     imagePicker.dismissViewControllerAnimated(true, completion: nil) 
     var photo = info[UIImagePickerControllerOriginalImage] as? UIImage 
     bedroomCells[lastSelectedIndex!.row].image = photo // Set the image for the selected index 
     tableView.reloadData() // Reload table view 
    } 

} 
+0

這是完美的,非常感謝你! – Cassandra 2015-04-06 20:37:41

相關問題