基于上一篇的滑动列表,动态添加/删除功能

简单保存下记录:

--- 增加一个数据项
function M:addItem(index)
    if index > self._count then
        return
    end
    self._picCount = self._picCount + 1
    self._count = self._picCount + 1
    for k,v in pairs(self._itemList) do
        local item = v
        if item._data >= index then
            local data = item._data + 1
            local col  = Mathf.Ceil(data / self._rowNum)
            local row  = data - (col - 1) * self._rowNum
            item:updateData(row, col)
            item._rectTF.anchoredPosition = self:getPosition(row, col)
        end
    end
    local col  = Mathf.Ceil(index / self._rowNum)
    local row = index - (col - 1) * self._rowNum
    self:createItem(row, col)
    local name = os.date("%Y%m%d%H%M%S")..math.ceil(Time.time * 10)
    DataUtil.addDrawPic(index, name)
    --- 行列适配
    self:adaptFixedCol()
    --- 大小适配
    self:adaptCount()
    self._scrollRect.horizontalNormalizedPosition = 1
end

--- 减少一个数据项
function M:delItem(index)
    local maxIndex = -1
    self._picCount = self._picCount - 1
    self._count = self._picCount + 1
    --- 行列适配
    self:adaptFixedCol()
    --- 大小适配
    self:adaptCount()
    for i = #self._itemList, 1, -1 do
        local item = self._itemList[i]
        if item._data == index then
            DataUtil.delDrawPic(index)
            --- 删除文件
            table.remove(self._itemList, i)
            UnityEngine.GameObject.Destroy(item.gameObject)
        elseif item._data > index then
            local data = item._data - 1
            local col  = Mathf.Ceil(data / self._rowNum)
            local row  = data - (col - 1) * self._rowNum
            item:updateData(row, col)
            item._rectTF.anchoredPosition = self:getPosition(row, col)
        end
        if item._data > maxIndex then
            maxIndex = item._data
        end
    end
    if maxIndex < self._count then
        local index = maxIndex + 1
        local col  = Mathf.Ceil(index / self._rowNum)
        local row  = index - (col - 1) * self._rowNum
        self:createItem(row, col)
    end
    self:onValueChanged()
end

 

posted @ 2022-04-28 11:28  -XZY  阅读(39)  评论(0编辑  收藏  举报