列表中的奇怪按钮行为(SwiftUI)

时间:2020-07-25 11:31:34

标签: ios swift button swiftui swiftui-list

在下面的SwiftUI代码中,我注意到了一些意外的行为。

我想知道这是否是错误,是否正常,或者我只是缺少明显的东西。

List {
    ForEach(self.myList, id: \.self.name) {
        item in
        HStack {
            Spacer()
            Button(action: {
                print("Button One tapped!")
                ....
            }) {
                item.name.map(Text.init)
                    .font(.largeTitle)
                    .foregroundColor(.secondary)
            }
            Spacer()
            Button(action: {
                print("Button Two tapped!")
                ....
            }) {
                Image(systemName: "pencil.circle")
                    .font(.title)
                    .foregroundColor(.secondary)
                    .padding(.leading, 17)
            }
        }
    }
    .onDelete(perform: deleteFunc)
}

现在这是在连续点击两个按钮之一时发生的情况。 我可以看到以下两条消息:

Button One tapped!
Button Two tapped!

我希望只看到一条消息,具体取决于所点击的按钮。

如果消息的顺序根据所点击的按钮而有所不同;我可以使用一两个布尔值来强制执行我想要的最终结果。但是两条消息总是以相同的顺序出现。

有人有同样的经历吗?还是有人看到任何错误?

1 个答案:

答案 0 :(得分:3)

使用library(data.table) library(stringr) # simulate data set.seed(123L) id=c(1:30) house_type=sample(c("3STR","2STR","1STR","DETC,1STR","2STR,DETC","OTHERS"),20, replace=TRUE) house=data.table(id,house_type) #> Warning in as.data.table.list(x, keep.rownames = keep.rownames, check.names #> = check.names, : Item 2 has 20 rows but longest item has 30; recycled with #> remainder. #OP house[,story:="others"][str_detect("3STR",house_type),story:="3STR"][ str_detect("2STR",house_type),story:="2STR"][ str_detect("1STR",house_type),story:="1STR"] ## Solutions house[, story2 := as.integer(sub("\\D*(\\d+).*", "\\1", house_type))] #> Warning in eval(jsub, SDenv, parent.frame()): NAs introduced by coercion house[, story3 := fifelse(is.na(story2), "other", paste0(story2, "STR"))] house #> id house_type story story2 story3 #> <int> <char> <char> <int> <char> #> 1: 1 1STR 1STR 1 1STR #> 2: 2 OTHERS others NA other #> 3: 3 1STR 1STR 1 1STR #> 4: 4 2STR 2STR 2 2STR #> 5: 5 2STR 2STR 2 2STR #> 6: 6 OTHERS others NA other #> 7: 7 1STR 1STR 1 1STR #> 8: 8 2STR,DETC others 2 2STR #> 9: 9 DETC,1STR others 1 1STR #> 10: 10 OTHERS others NA other #> 11: 11 OTHERS others NA other #> 12: 12 3STR 3STR 3 3STR #> 13: 13 2STR 2STR 2 2STR #> 14: 14 1STR 1STR 1 1STR #> 15: 15 2STR,DETC others 2 2STR #> 16: 16 1STR 1STR 1 1STR #> 17: 17 1STR 1STR 1 1STR #> 18: 18 3STR 3STR 3 3STR #> 19: 19 DETC,1STR others 1 1STR #> 20: 20 3STR 3STR 3 3STR #> 21: 21 1STR 1STR 1 1STR #> 22: 22 OTHERS others NA other #> 23: 23 1STR 1STR 1 1STR #> 24: 24 2STR 2STR 2 2STR #> 25: 25 2STR 2STR 2 2STR #> 26: 26 OTHERS others NA other #> 27: 27 1STR 1STR 1 1STR #> 28: 28 2STR,DETC others 2 2STR #> 29: 29 DETC,1STR others 1 1STR #> 30: 30 OTHERS others NA other #> id house_type story story2 story3 (或任何自定义样式),因为列表会自动检测默认按钮样式以突出显示整个行。

这是一个简化的(来自您的代码)演示:

PlainButtonStyle