UITutViewCell内的UIButton面向图像更改问题,同时tableview滚动 [英] UIButton inside UITableViewCell facing image changing issue while tableview get scrolled

查看:107
本文介绍了UITutViewCell内的UIButton面向图像更改问题,同时tableview滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SWIFT添加到收藏页面。在我的产品列表中,有Wish按钮。该按钮是从Storyboard拖动的。如果用户单击该按钮,则该图像将更改为另一个新图像。我做到了这一部分,完全正常工作。但是,如果我们滚动tableview,则自动将另一个单元格按钮图像更改为新图像。我不知道怎么样,为什么?怎么解决这个?请指导我。

I am doing "Add to favourite Page" using SWIFT. In my product list table, "Wish" button is there. That button is dragged from Storyboard. If User clicks the button, that image change to another new one. I did upto this part, exactly working too. But, if we scroll the tableview, automatically another cell button image has been changed to new one. I don't know how and why? How to solve this? Kindly guide me.

//MY CODING IS BELOW
unc tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell1 = product_tableview.dequeueReusableCellWithIdentifier("productmain", forIndexPath: indexPath) as product_tblCell
           image_url = arrayFromJson[indexPath.row].valueForKey("image_url") as NSString
           let imgurl = NSURL(string: image_url)!
           var imgdata : NSData = NSData(contentsOfURL: imgurl)!
           prod_img = UIImage(data: imgdata)!

           id = String(arrayFromJson[indexPath.row].valueForKey("id") as NSInteger)
           code = arrayFromJson[indexPath.row].valueForKey("code") as NSString
           descrip = arrayFromJson[indexPath.row].valueForKey("description") as NSString
           name = arrayFromJson[indexPath.row].valueForKey("name") as NSString
           price = String(arrayFromJson[indexPath.row].valueForKey("price") as NSInteger)

           cell1.name.text = name
           cell1.prod_desc.text = descrip
           cell1.prod_price.text = price
           cell1.prod_image.image = prod_img


           cell1.wish_image_but.addTarget(self, action : Selector("to_wish_list:"), forControlEvents: UIControlEvents.TouchUpInside)

           return cell1
}

func to_wish_list(sender : UIButton)
   {
           var img = UIImage(named: "wish_2.png")
           sender.setImage(img, forState: UIControlState.Normal)
           sender.enabled = false
   }


推荐答案

原因是当你滚动tableview并且你的单元格超出可见边界时,它用于显示另一个数据。当它返回时是可见的边界,它的数据再次被设置。

The reason is when you scroll the tableview and your cell goes beyond visible boundry, it is used for displaying another data. And when it has comes back is visible boundry it's data is set again.

这意味着,你必须在设置单元格时检查单元格是否已被标记为to_wish_list数据。如果是,则默认设置UIImage(名称:wish_2.png),否则设置defult image。

That means, you have to check if the cell has been marked for "to_wish_list" while setting cell data. if yes then set the UIImage(named: "wish_2.png") by default else set defult image.

这篇关于UITutViewCell内的UIButton面向图像更改问题,同时tableview滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆