[英]Swift - insert item in TableView from another ViewController
I am having a problem where I seem to add items to a tableView
in another ViewController
but they don't appear if presenting the VC.我遇到了一个问题,我似乎将项目添加到另一个
ViewController
的tableView
,但如果呈现 VC,它们不会出现。
I am also having different tableViews
which makes it a bit more difficult:我也有不同的
tableViews
这使得它有点困难:
This is inside my first ViewControllerA
from where I can access the 2nd ViewControllerB
:这是在我的第一个
ViewControllerA
中,我可以从中访问第二个ViewControllerB
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// if indexPath.item is less than data count, return a "Content" cell
if indexPath.item < wishListTitlesArray.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.cellLabel.text = wishListTitlesArray[indexPath.item]
cell.buttonView.setImage(wishListImagesArray[indexPath.item], for: .normal)
cell.customWishlistTapCallback = {
// track selected index
self.currentWishListIDX = indexPath.item
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as! WishlistViewController
// set image
vc.wishlistImage.image = self.wishListImagesArray[indexPath.item]
// set label
vc.wishlistLabel.text = self.wishListTitlesArray[indexPath.item]
// set wishlist
vc.theTableView.wishList = self.userWishListData[indexPath.item]
vc.currentWishListIDX = self.currentWishListIDX
vc.userWishListData = self.userWishListData
vc.theTableView.tableView.reloadData()
self.present(vc, animated: true, completion: nil)
}
return cell
}
func insertWish(){
insertWishDelegate?.insertWish(wishName: popUpView.whishName!, idx: selectedWishlistIDX!, currentWishlistIDX: currentWishListIDX,data: userWishListData)
// save Wish to database -> DataHandler
saveWish()
}
And this is inside my 2nd ViewController:这是在我的第二个 ViewController 中:
func insertWish(wishName: String, idx: Int, currentWishlistIDX: Int, data: [[Wish]]) {
// append the new wish to the user's currently selected wishlist
var wishes: [Wish] = data[idx]
wishes.append(Wish(withWishName: wishName, checked: false))
userWishListData[currentWishlistIDX] = wishes
// set the updated data as the data for the table view
theTableView.wishList = userWishListData[currentWishlistIDX]
theTableView.tableView.reloadData()
}
Presenting the 2nd ViewController works just fine.呈现第二个 ViewController 工作得很好。 All the variables I pass are the right ones.
我传递的所有变量都是正确的。 The only problem I have is that I am not able to insert a "Wish".
我唯一的问题是我无法插入“愿望”。 There is no error or something.
没有错误什么的。 The user can add Wishes from the first ViewController:
用户可以从第一个 ViewController 添加愿望:
@objc func wishButtonTapped(){
dismissPopUpView()
insertWish()
}
For the whole project you can look at my git (very messy though): Git Project对于整个项目,您可以查看我的 git(虽然很乱): Git Project
I probably miss something very basic but I can not get behind what it is so I am very grateful for every help :)我可能会错过一些非常基本的东西,但我无法理解它是什么,所以我非常感谢每一个帮助:)
Your code cannot work because you are accessing UI elements in vc
which are not connected (yet) right after instantiating the view controller.您的代码无法工作,因为您正在访问
vc
中的 UI 元素,这些元素在实例化视图控制器后尚未连接(尚未)。
Secondly you are strongly discouraged from using multiple arrays for the data source.其次,强烈建议您不要为数据源使用多个数组。 Never do that.
永远不要那样做。 Create a custom struct to hold all parameters of one item (in the code below called
Item
) and use one data source array (called datasourceArray
).创建一个自定义结构来保存一项的所有参数(在下面的代码中称为
Item
)并使用一个数据源数组(称为datasourceArray
)。
Then assign the item to a temporary variable ( wishList
in vc
) and assign the values to the UI elements in viewDidLoad
然后将该项目分配给一个临时变量(
vc
wishList
)并将值分配给viewDidLoad
的 UI 元素
This example assumes that the values from the three arrays ( image
, title
and data
) are in one instance of the custom struct.此示例假定来自三个数组(
image
、 title
和data
)的值位于自定义结构的一个实例中。 You have to redesign the entire data model.您必须重新设计整个数据模型。 In the closure pass the
Item
instance to vc
在闭包中将
Item
实例传递给vc
cell.customWishlistTapCallback = {
// track selected index
self.currentWishListIDX = indexPath.item
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as! WishlistViewController
vc.wishlist = datasourceArray[indexPath.item]
vc.currentWishListIDX = self.currentWishListIDX
vc.userWishListData = self.userWishListData
self.present(vc, animated: true, completion: nil)
}
and in vc in viewDidLoad
populate the UI elements and reload the table view并在 vc 中的
viewDidLoad
填充 UI 元素并重新加载表格视图
var wishList : Item!
override func viewDidLoad()
super.viewDidLoad()
wishlistImage.image = wishList.image
// set label
wishlistLabel.text = wishList.title
// set wishlist
theTableView.wishList = wishList.data
theTableView.tableView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.