HarmonyOS鸿蒙Next中foreach循环数据变了,视图没变,第三个参数也加了

HarmonyOS鸿蒙Next中foreach循环数据变了,视图没变,第三个参数也加了

foreach 循环数据变了,视图没变,第三个参数也加了

图片并不会根据item.selected 更换

Row() {
    ForEach(this.listPhotoPixelMap, (item: photoItem,index:number) => {
        Stack({alignContent: Alignment.BottomEnd}) {
            Image(item.pixelMap)
                .width('100%')
                .height('100%')
                .objectFit(ImageFit.Cover)
            if (this.photoSelect) {
                if (!item.selected) {
                    Image($r('app.media.uu_sc_ic_unselected'))
                        .width(20)
                        .aspectRatio(1)
                } else {
                    Image($r('app.media.uu_sc_ic_selected'))
                        .width(20)
                        .aspectRatio(1)
                }
            }
        }
        .width('22%')
        .margin({right: 4,bottom:4})
        .aspectRatio(1)
        .onClick(() => {
            item.selected = !item.selected
            // this.indexArr.push(index)
        })
    },(item:photoItem,index: number) => {
        return item.key + Math.random() + ''
    })
}
.justifyContent(FlexAlign.Start)
.layoutWeight(1)

更多关于HarmonyOS鸿蒙Next中foreach循环数据变了,视图没变,第三个参数也加了的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

item.selected = !item.selected
这句代码不会改变原来的数组,也不会更新视图

更多关于HarmonyOS鸿蒙Next中foreach循环数据变了,视图没变,第三个参数也加了的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


姓名: 张三
职业: 软件工程师
简介: 拥有超过10年的IT行业经验,擅长Java和Python开发。

在HarmonyOS鸿蒙Next中,如果foreach循环的数据发生变化但视图未更新,可能是由于数据绑定机制未正确触发。确保数据源是响应式的,或者手动调用notifyDataChanged方法强制刷新视图。第三个参数通常用于指定唯一标识符,确保其唯一性以避免视图复用问题。

回到顶部