Reputation: 41
I need to set last cell as button that can add new cell to this array of cells. here is some my code
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let category = self.categories[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.name, for: indexPath) as! CategoryCollectionViewCell
cell.label.text = category.name
return cell
}
private var categories = [Category]()
private func createCustomCategories() {
let categoryOne = Category(id: "01", name: "B")
let categoryTwo = Category(id: "02", name: "Weddings")
let categoryThree = Category(id: "03", name: "Just want it")
let addButton = Category(id: "none", name: "+ add")
self.categories.append(contentsOf: [categoryOne, categoryTwo, categoryThree])
self.categories.append(addButton)
}
Upvotes: 0
Views: 1008
Reputation: 578
Create both a CustomCell
Class to represent your categories, and another ButtonCell
Class to represent your button,
assuming this is your items array:
var items = [1,2,3,4,5]
in your dataSource funcs add +1 for your button
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == items.count {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "\(ButtonCell)", for: indexPath) as? UITableViewCell else {return UITableViewCell()}
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "\ .
(YourSelf)", for: indexPath) as? UITableViewCell else {return UITableViewCell()}
}
}
Upvotes: 2
Reputation: 41
I made this but I always get index our of range
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categories.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let category = categories[indexPath.item]
if indexPath.item == categories.count + 1 {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.name, for: indexPath) as? CategoryCollectionViewCell else { return UICollectionViewCell.init() }
cell.setText(text: "add")
return cell
} else {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.name, for: indexPath) as? CategoryCollectionViewCell else {return UICollectionViewCell.init()}
cell.setText(text: category.name!)
return cell
}
}
Upvotes: 0
Reputation: 2240
Why not just add the button with unique ID to categories ?
let addButton = Category(id: "UniqueID", name: "+ add")
// append the addButton in category array
self.categories.append(contentsOf: [categoryOne, categoryTwo, categoryThree, addButton])
And then just switch on the Id, And also have a separate cell for button.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let category = self.categories[indexPath.row]
// check for category.id == your uniqueID that you have appended above
if category.id == "UniqueID" {
// Make sure you have another collectionViewCell and use its identifier in here
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:AddButtonCell.name, for: indexPath) as! AddButtonCell
// set the label here
cell.label.text = category.name
return cell
} else {
// use your original collectionview cell here.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.name, for: indexPath) as! CategoryCollectionViewCell
cell.label.text = category.name
return cell
}
}
Upvotes: 1
Reputation: 1158
You can do it like this :
at numberOfItemsInSection
return self.categories.count +1
And at cellForRow
:
if indexPath.item > categories.count{ //last element of categories array is passed
cell.label.text = "+ add"
}
Hope it helps...
Upvotes: 1