I'm a student iOS dev, and I'm trying to control a tableview in a collection view cell that is returning 3 (or more) tableviews so I can have multiple tableviews. I believe I implemented everything right but no data is returned to the individual tableviews I have set the reuseidentifiers in the prototype cells in the tableview, and also the delegate and datasource are set to the VC.
var tableView1: UITableView?
var tableview2: UITableView?
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
if tableView == tableView1 {
return 2;
} else if tableView == tableview2 {
return 3
}
return 0;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == tableView1 {
return 2;
} else if tableView == tableview2 {
return 1;
}
return 0;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if tableView == tableView1 {
cell = tableView.dequeueReusableCellWithIdentifier("testcell1", forIndexPath: indexPath) as! UITableViewCell
} else if tableView == tableview2 {
cell = tableView.dequeueReusableCellWithIdentifier("testcell2", forIndexPath: indexPath) as! UITableViewCell
}
// Configure the cell...
if tableView == tableView1 {
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
} else if tableView == tableview2 {
cell.textLabel?.text = "Test Table 2 "
cell.detailTextLabel?.text = "1:30 PM - 2:30 PM"
cell.selectionStyle = .None
}
return cell
}
//**Center collection cells in the middle**
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let sideInset = (collectionView.frame.size.width - 650) / 2
return UIEdgeInsets(top: 0, left: sideInset, bottom: 0, right: sideInset)
}
}
//Card Scrolling datasource
extension SAHomeViewController: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
//Number of cards on home screen
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cardcell", forIndexPath: indexPath)
// Configure collection view cell
return cell
}
Here is my project editor to be clearer.
You need to provide default return value in both the functions. Because compiler checking that the functions required Int value should be returned and in these functions if any condition doesn't matched it will not return anything.
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
if tableView == tableView1 {
return 2;
}
else if tableView == tableview2
{
return 3;
}
return 0; // here
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == tableView1 {
return 2;
} else if tableView == tableview2 {
return 1;
}
return 0; // here
}
Related
I have a table view nested in a collection view and i'm returning 3 (possibly more in the future) collection view cells and I was wondering if it is possible to present different content in each one of the collection cells? I attached a few screenshots to better understand what I am taking about. Thanks.
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
return cell
}
Yes you can. You need set a property for every tableView you have and in delegate method compare it like below
class Some: UIViewController {
var firstTableView: UITableView
var secondTableView: UITableView
override func viewDidLoad() {
firstTableView = YOUR_FIRST
secondTableView = YOUR_Second
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if tableView == firstTableView {
return 2;
}
else if tableView == secondTableView {
return 1;
}
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == firstTableView {
return 2;
}
else if tableView == secondTableView {
return 1;
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
if tableView == firstTableView {
cell = tableView.dequeueReusableCellWithIdentifier("cellOfFirstTableView", forIndexPath: indexPath) as! UITableViewCell
}
else if tableView == secondTableView {
cell = tableView.dequeueReusableCellWithIdentifier("cellOfSecondTableView", forIndexPath: indexPath) as! UITableViewCell
}
// Configure the cell...
if tableView == firstTableView {
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
}
else if tableView == secondTableView {
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
}
return cell
}
}
You can use UITableViewDelegate / UITableViewDataSource methods with if else conditions or some thing similar
eg.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table1 { // table1 is a global var for the table
} else if tableView == table2 {
}
}
But I think it will be very clear if you use separate controller class for each table so you can easily manage the code.
But this depends on what type of data you have. If data is completely unrelated you can just use 3 different controllers.
Or if you can reuse data and codes among 3 tables then you can decide if you wanna use 3 different controllers or to use i class with above method.
eg.
let table1Controller = Table1Controller(dataList1)
let table2Controller = Table2Controller(dataList2)
let table3Controller = Table3Controller(dataList3)
table1.delegate = table1Controller
table1.dataSource = table1Controller
table2.delegate = table2Controller
table2.dataSource = table2Controller
table3.delegate = table3Controller
table3.dataSource = table3Controller
I have multiple sections in my TableView and I'm a bit stuck to display there names in the correct section. I'm new to xcode, so this an easy one for most but not for me :s
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 0 {
return areas.bars.count
} else {
return areas.clubs.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("barsandclubsIdentifier", forIndexPath: indexPath)
if section == 0 { // **This is where I'm stuck I can't put section -> UITableViewCell**
let bars = areas.bars
let bar = bars[indexPath.row]
cell.textLabel?.text = bar.name
return cell
} else {
let clubs = areas.clubs
let club = clubs[indexPath.row]
cell.textLabel?.text = club.name
}
}
Try this may help you :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("barsandclubsIdentifier", forIndexPath: indexPath)
if indexPath.section == 0 { // **This is where I'm stuck I can't put section -> UITableViewCell**
let bars = areas.bars
let bar = bars[indexPath.row]
cell.textLabel?.text = bar.name
}else {
let clubs = areas.clubs
let club = clubs[indexPath.row]
cell.textLabel?.text = club.name
}
return cell
}
I have successfully implemented two different sections in my table view. Section 1 has 3 cells displayed first, then section 2 has 12 displayed afterwards.
I would like to have it ordered so section 1 has its 3 cells mixed into section 2 (12 cells) So in this case it would be displayed every 4 cells. I would like to code it in a way that when the section 2 cells increases over time it keeps section 1 every 4 cells.
Here is the tableview delegate functions I have at the moment
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if (section == 0) {
return 3 // At the moment I have hard coded it will change it to array.count
} else {
return eventNameArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
// Do something!
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
// Do something!
return cell
}
}
You cannot mix the cells of different sections.
Since that is what you want the solution is to remove the sections all together or include more sections:
Solution 1:
1 Section with x cells: Cell|Cell|Cell|Cell|MovingCell|Cell|Cell|Cell|Cell|Moving Cell
Solution 2: y Sections with 1 or z cells - Cell|Cell|Cell|Cell + MovingCell + Cell|Cell|Cell|Cell + Moving Cell
I will show you the code that would be needed for Solution 1. You will need to have some variable indicating what the length of Cell-semi-sections would be, e.g. let cellCountBeforeMoving = 4:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3 + eventNameArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.item + 1) % (cellCountBeforeMoving + 1) == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
// Do something!
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
// Do something!
return cell
}
}
Note the indexes are now a little offset and you have to be careful accessing the array. The probably correct way to get the element corresponding to a given indexPath is
eventNameArray[indexPath.item - ((indexPath.item + 1) / (cellCountBeforeMoving + 1))]
Solution 1 will end up something like: (you need some var which gives you the number of interplaced MovingCells)
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return movingCells * 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section % 2 == 1 {
return 1
}
return cellCountBeforeMoving
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section % 2 == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
// Do something!
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
// Do something!
return cell
}
}
I'm trying to add a UITableViewCell to another UITableView section whenever a button on the cell is tapped. However, I'm quite confused about the process of how to change a cell's section location after it has already been loaded into the table view. Currently I have two sections and am adding 5 custom UITableViewCells into the first section.
Any ideas on how to move the cells to the second section on tap?
Here are cell and section methods in my view controller class:
var tableData = ["One","Two","Three","Four","Five"]
// Content within each cell and reusablity on scroll
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var tableCell : Task = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Task
tableCell.selectionStyle = .None
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
var titleString = "Section \(indexPath.section) Row \(indexPath.row)"
tableCell.title.text = titleString
println(indexPath.row)
return tableCell
}
// Number of sections in table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
// Section titles
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "First Section"
} else {
return "Second Section"
}
}
// Number of rows in each section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return tableData.count
} else if section == 1 {
return 0
} else {
return 0
}
}
You need to have a separate datasource for first and second sections. When button is tapped, modify datasource and move cell to new section with moveRowAtIndexPath(indexPath: NSIndexPath, toIndexPath newIndexPath: NSIndexPath) UITableView method.
For example:
var firstDataSource = ["One","Two","Three","Four","Five"]
var secondDataSource = [ ]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return section == 0 ? firstDataSource.count : secondDataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = indexPath.section == 0 ? firstDataSource[indexPath.row] : secondDataSource[indexPath.row]
return cell
}
// For example, changing section of cell when click on it.
// In your case, similar code should be in the button's tap event handler
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if indexPath.section == 0
{
let data = firstDataSource[indexPath.row]
tableView.beginUpdates()
secondDataSource.append(data)
firstDataSource.removeAtIndex(indexPath.row)
let newIndexPath = NSIndexPath(forRow: find(secondDataSource, data)!, inSection: 1)
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
tableView.endUpdates()
}
}
I want the numberOfRowsInSection to return the number of comment at index path and one post only in the first cell this return one cell for post and one for comment neglecting the number of comment in the var.
What's wrong?
Update :
This is a details view controller : when user tap the post to view comments it present the post in the first cell and the comments down.
I got a problem the first cell is ok but the comments cell present only one cell no matter how many string in the variable what's wrong ?
var comments = ["comments","comments","comments"]
var posts = ["posts"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
cell.textLabel?.text = "fff"
}
let cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
// Configure the cell...
cell.textLabel?.text = comments[indexPath.row]
return cell
}
I tried this one too > didn't work
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if (section == 0) {
return 1
}
return comments.count
}
this is the simulator output
If you want your post and comments to be cells in a single section, then your numberOfRowsInSection should be posts.count + comments.count.
If you want two sections (one for the post and one for the comments), you need to return 2 from numberOfSectionsInTableView: and have your tableView:numberOfRowsInSection: look like your edit
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return posts.count
}
return comments.count
}
Judging by your last update, this is the solution I think you are looking for:
var comments = ["comments","comments","comments"]
var posts = ["posts"]
override func viewDidLoad() {
super.viewDidLoad()
Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 2 //One Section for the post and One Section for the comments?
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if (section == 0) {
return 1
}
return comments.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//post's section == 0
var cell: UITableViewCell!
if indexPath.section == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
cell.textLabel?.text = "fff"
}
else {
cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
// Configure the cell...
cell.textLabel?.text = comments[indexPath.row]
}
return cell
}
Finally !
class CommentVC: UIViewController {
var comments = ["comments","comments","comments","comments","comments","comments"]
var posts = ["posts","posts"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//
// func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// // #warning Potentially incomplete method implementation.
// // Return the number of sections.
// return 1
// }
//
// func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// // #warning Incomplete method implementation.
// // Return the number of rows in the section.
//
// if (section == 0) {
// return posts.count
// }
// return comments.count
// }
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 2//One Section for the post and One Section for the comments?
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if (section == 0) {
return 1
}
return comments.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//post's section == 0
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
cell.textLabel?.text = "fff"
}
let cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
// Configure the cell...
cell.textLabel?.text = comments[indexPath.row]
return cell
}