将 ScrollView 与数组一起使用时出现问题
Issue using ScrollView with Array
我正在尝试使用数组来显示一些游戏信息,只要通过调用网络服务更新数组。数组填充正常,但在运行时我得到:
Generic struct 'ForEachWithIndex' requires that 'Binding<[MatchData]>'
conform to 'RandomAccessCollection'
我的滚动视图:
ScrollView{
ForEach(GameCenterHelper.helper.$MatchList, id: \.self) {row in
ext("\(row.id)")
}
}
我的数组声明:
@State var MatchList: [MatchData] = [MatchData]()
和匹配数据:
class MatchData : Identifiable {
var id: String
var LocalPlayer: Player
var RemotePlayer: Player
init(_ match: GKTurnBasedMatch) {
let local = match.participants[0].player!
let remote = match.participants[1].player ?? GKPlayer()
self.id = match.matchID
LocalPlayer = Player(Alias: local.alias
, DisplayName: local.displayName
, TeamPlayerId: local.teamPlayerID
, PlayerId: local.gamePlayerID
)
RemotePlayer = Player(Alias: remote.alias
, DisplayName: remote.displayName
, TeamPlayerId: remote.teamPlayerID
, PlayerId: remote.gamePlayerID
)
}
}
我不得不承认,这是我第一次使用状态来尝试刷新 ScrollView,但我发现它比我预期的要难得多,而且我不确定我的代码有什么问题.
您不需要绑定在 ForEach
中迭代 MatchList
,直接访问它
ScrollView{
ForEach(GameCenterHelper.helper.MatchList, id: \.self) {row in // << no $
ext("\(row.id)")
}
}
我正在尝试使用数组来显示一些游戏信息,只要通过调用网络服务更新数组。数组填充正常,但在运行时我得到:
Generic struct 'ForEachWithIndex' requires that 'Binding<[MatchData]>' conform to 'RandomAccessCollection'
我的滚动视图:
ScrollView{
ForEach(GameCenterHelper.helper.$MatchList, id: \.self) {row in
ext("\(row.id)")
}
}
我的数组声明:
@State var MatchList: [MatchData] = [MatchData]()
和匹配数据:
class MatchData : Identifiable {
var id: String
var LocalPlayer: Player
var RemotePlayer: Player
init(_ match: GKTurnBasedMatch) {
let local = match.participants[0].player!
let remote = match.participants[1].player ?? GKPlayer()
self.id = match.matchID
LocalPlayer = Player(Alias: local.alias
, DisplayName: local.displayName
, TeamPlayerId: local.teamPlayerID
, PlayerId: local.gamePlayerID
)
RemotePlayer = Player(Alias: remote.alias
, DisplayName: remote.displayName
, TeamPlayerId: remote.teamPlayerID
, PlayerId: remote.gamePlayerID
)
}
}
我不得不承认,这是我第一次使用状态来尝试刷新 ScrollView,但我发现它比我预期的要难得多,而且我不确定我的代码有什么问题.
您不需要绑定在 ForEach
中迭代 MatchList
,直接访问它
ScrollView{
ForEach(GameCenterHelper.helper.MatchList, id: \.self) {row in // << no $
ext("\(row.id)")
}
}