2015-04-03 157 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

相关问题