Nicholas Piccoli Nicholas Piccoli - 4 months ago 9x
Swift Question

Prototype cell + image + delegate

So this is what happened: I created a tableView and delegated some stuff to it from another controller. The textLabels from the cells are working fine, but whenever I add

, all cells change to that specific image.

Any ideas on how to fix it? The textLabels are static but the images are not.

Here I receive the image to the tableView:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
let row = indexPath.row
let titulo = arrayInfo[row]
let image = imgR
cell.textLabel!.text = titulo
cell.imageView?.image = image

return cell

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "add") {
let view = segue.destinationViewController as! EsseVaiPassar
view.delegate = self
view.delegateimg = self

and here I pass it from the controller to the other one with the tableView:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
imagemRecebida.contentMode = .ScaleAspectFit
imagemRecebida.image = pickedImage

print("IMAGE PICKED: \(pickedImage)")

dismissViewControllerAnimated(true, completion: nil)

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)


The code is working as intended. For your textview, you are asking the value arrayInfo[row]. So for each cell as the indexPath.row changes the respective element from the array is displayed in the text field.

But for your image, it is just a single image and not an array. You aren't using indexPath.row to select a particular image.

To add different images to your tableView, just create an array of image like you have created an array of String. Just implement the logic you have implemented for the textView cell.imageView?.image = image[indexPath.row]

To add image only to the first cell and leave the remaining cells blank use this

if indexPath.row == 0
    cell.imageView?.image = image