gpt4 book ai didi

ios - swift 用新元素更新表格 View

转载 作者:行者123 更新时间:2023-11-29 02:17:17 24 4
gpt4 key购买 nike

因此,我在 numberOfRowsInSection 中有这段代码,用于根据按下的侧边栏选项(不同的 tableView)来更改显示的数量。当用户选择索引 #1 时,他们应该只能看到提取请求中存在的行数。当我更改索引时,不会更新任何内容,我猜测这是因为它没有被刷新/更新。我应该使用什么代码来正确更新它以及它应该放置在哪里?

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if sidebarindex == 3{
return feeds.count
} else if sidebarindex == 2{
return feeds.count
} else if sidebarindex == 1{

let moc = SwiftCoreDataHelper.managedObjectContext()
var favNames: [String] = []
let fetchRequestM = NSFetchRequest(entityName:"Favorite")
if let favs = moc.executeFetchRequest(fetchRequestM, error: nil) as? [Favorite] {
favNames = favs.map { $0.favoriteTitle }}

if favNames.count != 0{
return favNames.count
}else{
return 1
}

} else{
return feeds.count
}



}

最佳答案

注意:IOS 和 Swift 的初学者,所以如果我错了请告诉我。但这是我试图回答你的问题:

用于刷新 tableView 中的数据的代码是:

tableView.reloadData()

在没有看到代码的情况下,我无法确切地说出将其放置在何处,但有根据的猜测是将其放置在按下侧边栏项目时激活的函数中。示例:

@IBAction func sideBarItem1Pressed(sender: UIButton) {
// Set the index here
tableView.reloadData()
}

如果您使用您的代码更新您的帖子,我也许能够确定它的去向。

关于ios - swift 用新元素更新表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com