【问题标题】:UIButton inside UITableViewCell facing image changing issue while tableview get scrolledUITableViewCell内的UIButton在tableview滚动时面临图像更改问题
【发布时间】:2015-04-03 11:41:15
【问题描述】:

我正在使用 SWIFT 进行“添加到收藏页面”。在我的产品列表中,有“Wish”按钮。该按钮是从情节提要中拖动的。如果用户单击该按钮,该图像将更改为另一个新图像。我做到了这部分,也确实在工作。但是,如果我们滚动 tableview,另一个单元格按钮图像会自动更改为新的。我不知道如何以及为什么?如何解决这个问题?请指导我。

//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
   }

【问题讨论】:

    标签: uitableview swift ios8


    【解决方案1】:

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

    这意味着,您必须在设置单元格数据时检查单元格是否已标记为“to_wish_list”。如果是,则默认设置 UIImage(named: "wish_2.png") 否则设置默认图像。

    【讨论】:

    • 怎么做?有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多