小编Dan*_*anå的帖子

如何在WatchOS的SwiftUI列表中设置行样式?

我正在使用SwiftUI,正在尝试在WatchOS的列表视图中创建具有自定义.buttonStyle的按钮列表,但无法使其正常工作。目前甚至可能吗?如果可以,怎么办?

示例项目:

struct Superhero: Identifiable {
    var id = UUID()
    var name: String
}

var superheroes = [
    Superhero(name: "Batman"),
    Superhero(name: "Superman"),
    Superhero(name: "Wonder Woman"),
    Superhero(name: "Aquaman"),
    Superhero(name: "Green Lantern"),
    Superhero(name: "The Flash")
]

struct SuperheroButtonStyle: ButtonStyle {

  func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
        .frame(minWidth: 0, maxWidth: .infinity)
        .foregroundColor(.white)
        .background(configuration.isPressed ? Color.white.opacity(0.95) : .green)
        .cornerRadius(13.0)
  }
}

struct SuperHeroesView: View{
    var body: some View {
        List {

            ForEach(superheroes) { superhero in
                Button(action: {
                    print(superhero.name)
                }){
                    Text(superhero.name)
                }.buttonStyle(SuperheroButtonStyle())
           } …
Run Code Online (Sandbox Code Playgroud)

apple-watch watchkit swiftui

6
推荐指数
4
解决办法
133
查看次数

如何最好地在 for-in 循环中进行空检查?

在“for-in”循环中检查 Iterable 是否为空的最佳方法是什么?在 forEach 循环中,您可以简单地执行以下操作:

people?.forEach((person) {
   print(person.name);
});
Run Code Online (Sandbox Code Playgroud)

?.将确保如果循环只运行people不为空。是否也可以在“for-in”循环中以一种很好的方式做到这一点?或者你需要做这样的事情:

if (people != null) {
   for (person in people) {
      print(person.name);
   }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我仅使用 print 作为示例,循环中完成的实际工作会更复杂,我想完全避免 forEach 循环。

似乎应该有更好的方法,但我找不到任何好的例子。

dart flutter

1
推荐指数
1
解决办法
1448
查看次数

标签 统计

apple-watch ×1

dart ×1

flutter ×1

swiftui ×1

watchkit ×1