更新cellForRowAtIndexPath中的单元格并不起作用

时间:2016-07-17 20:08:51

标签: swift swift2

我有一个UITableView,允许我在选择父行时拥有子行,以便扩展。

这是我的父行:

enter image description here

如果我选择该行,则会展开:

enter image description here

我想让子行元素缩进一点,所以它看起来真的是下面的项目是孩子。

在我的UITableViewCell中,我在为单元格设置图标和标题时添加了此代码:

func setData(data: ExpandableCell,indexPath : NSIndexPath)
    {

        self.itemImg.image = UIImage(named: data.icon)
        self.itemTitle.text = data.title

        if(!data.isParent){

            // indent views
            self.itemImg.frame.origin.x = 53
            self.itemTitle.frame.origin.x = 85
        }

        if(data.childs.count == 0){

            // remove the expand - collpase icon when its child row
            self.expandImg.removeFromSuperview()
        }

    }

我遇到的问题是,第一次显示子行时,它不会缩进图标和标签。它一直工作到第二次:

  

第一次显示子行(没有工作):

enter image description here

  

第二次显示子行(这次有效!

enter image description here

我必须设置一些东西,以便它始终有效吗?我已经做过:SetNeedDisplay()但是基本有相同的行为。

1 个答案:

答案 0 :(得分:1)

问题在于你的逻辑是不完整的:

data.isParent

很好,但如果if (MessageBox.Show("Are you sure to exit. Confirm?", "Rest O Rant", MessageBoxButtons.YesNo) == DialogResult.Yes) { Application.Exit(); MessageBox.Show("Thank You For Using Rest O Rant Software.", "Closing message!", MessageBoxButtons.OK); } else { e.Cancel = true; this.Activate(); } 是真的怎么办?你不说那么做什么。请记住,细胞可以重复使用。您需要准备清空已填充的单元格并重新配置它。