swift UICollectionViewCell text is not appearing in UICollectionView with two custom cell - swift

I'm trying to make a timetable app with UICollectionView.
So I made a two custom cell class, TimeTableCell and TimeTableDescriptionCell.
TimeTableCell has two label to present the subject name and classroom.
TiemTableDescriptionCell is for the first section and the first row, to present the day of the week and the time.
I just simply dragged the UICollectionViewCell in UICollectionView in the storyboard, and designed two cells and connected each of them with TimeTableCell and TimeTableDescriptionCell.
The two cell's identifier is diffrent.
This is my collectionView(collectionView: , cellForItemAtIndexPath: )
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print("\(indexPath.section), \(indexPath.row)")
let dayArray = ["", "월", "화", "수", "목", "금"]
let cell: UICollectionViewCell!
if indexPath.section == 0 {
// Day
cell = collectionView.dequeueReusableCellWithReuseIdentifier("TimeTableDescriptionCell", forIndexPath: indexPath) as! TimeTableDescriptionCell
cell.contentView.layer.borderWidth = 1
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
(cell as! TimeTableDescriptionCell).titleLabel!.text = "월"
return cell
} else if indexPath.row == 0 {
// index
cell = collectionView.dequeueReusableCellWithReuseIdentifier("TimeTableDescriptionCell", forIndexPath: indexPath) as! TimeTableDescriptionCell
cell.contentView.layer.borderWidth = 1
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
(cell as! TimeTableDescriptionCell).titleLabel.text = "1"
return cell
} else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TimeTableCell
if let timeTableElement = self.timeTable[indexPath.section-1][indexPath.row-1] {
(cell as! TimeTableCell).subjectNameLabel.text = timeTableElement.subjectName
(cell as! TimeTableCell).subjectNameLabel.adjustsFontSizeToFitWidth = true
(cell as! TimeTableCell).classRoomLabel.text = timeTableElement.classRoom
(cell as! TimeTableCell).classRoomLabel.adjustsFontSizeToFitWidth = true
} else {
(cell as! TimeTableCell).subjectNameLabel.text = ""
(cell as! TimeTableCell).classRoomLabel.text = ""
}
cell.contentView.layer.borderWidth = 1
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
print("class")
return cell
}
return cell
}
and this is my storyboard.
my storyboard
When I debugg, the TimeTableCells are showing nice.
But the TimeTableDescriptionCells are not appearing like this.
debugg
In the first section and the first row, there should be the day of the week and the time.
How can I fix this?
EDIT:
My other functions of UICollectionViewDataSource is
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
if timeTableData == nil {
return 0
}
return self.timeTable.count+1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if timeTableData == nil {
return 0
}
return self.timeTable[0].count+1
}
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
}
}
I wan't to fill the first section with "Mon", "Tue"... and first row with 1,2,3,4,...

Related

Unable to access collectionView cell variables using global variable

Here i made a collectionView cell variable for accessing both objects. But unable to access the objects inside the cell variable
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell!
if collectionView == collectionView1 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
cell.imgAttachment.image = imageArray1[indexPath.row]
cell.delegate = self
}
else if collectionView == collectionView2 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
}
return cell
Value of type 'UICollectionViewCell?' has no member 'imgAttachment'
The problem lies here.
var cell: UICollectionViewCell!
You have declared the cell to be of type UICollectionViewCell. So, no matter which subclass you store inside it, you cell will only be of type UICollectionViewCell.
You should change it like this,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView === collectionView1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
cell.imgAttachment.image = imageArray1[indexPath.row]
cell.delegate = self
return cell
} else if collectionView === collectionView2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
return cell
} else {
// Return the proper cell for other cases
}
}
Or, if you are adamant you need only a single return statement at the end of the delegate, then you could do it like this,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var yourCell: UICollectionViewCell!
if collectionView === collectionView1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
cell.imgAttachment.image = imageArray1[indexPath.row]
cell.delegate = self
yourCell = cell
} else if collectionView === collectionView2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
yourCell = cell
} else {
// Return the proper cell for other cases
}
return yourCell
}
You need to silent the compiler by casting the cell as AttachmentCell. See the below example,
(cell as! AttachmentCell).imgAttachment.image = imageArray1[indexPath.row]
(cell as! AttachmentCell).delegate = self
The reason compiler is not able to recognize the variable is the declaration of variable cell as UICollectionViewCell. As there is no imgAttachment variable in UICollectionViewCell so compiler will complain.

How to set manual scroll position when collection view loads in swift?

I have the main table view where collection view is settled in table view section header because of the horizontal scroll view. when I click on collection view (row 1) table view reloads. but when I click on collection view row 5, it reloads the table view and shows the collection scroll position from the beginning. Please help me out to set the manual scroll position for collection view which is inside table view.
Here is code what I am using.
func numberOfSections(in tableView: UITableView) -> Int
{
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if section == 0
{
return 1
}
else
{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if indexPath.section == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "banTableViewCell", for: indexPath) as! banTableViewCell
return cell
}
else
{
if chkVal == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell
return cell
}
else if chkVal == 1
{
let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell
return cell
}
else if chkVal == 2
{
let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell
return cell
}
else if chkVal == 3
{
print("its cming gallery there")
let cell = tableView.dequeueReusableCell(withIdentifier: "containerTableViewCell", for: indexPath) as! containerTableViewCell
return cell
}
else
{
print("its cming calendar there")
let cell = tableView.dequeueReusableCell(withIdentifier: "galTableViewCell", for: indexPath) as! galTableViewCell
return cell
}
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
if section == 0
{
let view = UIView(frame: CGRect(x:0, y:0, width:0, height:0))
return view
}
else
{
let SegCell = tableView.dequeueReusableCell(withIdentifier: "segTableViewCell") as! segTableViewCell
SegCell.segcol.dataSource = self as UICollectionViewDataSource
SegCell.segcol.delegate = self as UICollectionViewDelegate
SegCell.segcol.isPagingEnabled = true
SegCell.segcol.isScrollEnabled = true
SegCell.segcol.showsHorizontalScrollIndicator = true
SegCell.segcol.register(UINib(nibName: "segCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "segCollectionViewCell")
// Initialization code
let indexPath = NSIndexPath(row: 3, section: 0)
SegCell.segcol.scrollToItem(at: indexPath as IndexPath, at: UICollectionViewScrollPosition.right, animated: false)
return SegCell
}
}
//Collection View Set
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "segCollectionViewCell", for: indexPath as IndexPath) as! segCollectionViewCell
if indexPath.row == 0
{
cell.TextSeg.text = "FITNESS REPORT CARD"
}
else if indexPath.row == 1
{
cell.TextSeg.text = "FITNESS STATS"
}
else if indexPath.row == 2
{
cell.TextSeg.text = "DIET & NUTRITION"
}
else if indexPath.row == 3
{
cell.TextSeg.text = "GALLERY"
}
else if indexPath.row == 4
{
cell.TextSeg.text = "CALENDAR"
}
return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width:150, height:46)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if indexPath.row == 0
{
tbl.reloadData()
}
else if indexPath.row == 1
{
tbl.reloadData()
}
else if indexPath.row == 2
{
tbl.reloadData()
}
else if indexPath.row == 3
{
tbl.reloadData()
}
else if indexPath.row == 4
{
tbl.reloadData()
}
}
It's not setting the scroll position in the collection view.
Thanks

How can I use switch statement to implement collectionView function in Swift?

I am new to swift and trying to implement collectionView function in a ViewController.Now I have three types of UICollectionViewCell,here is my code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewCategory {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell
cell.CategoryIcon.image = self.categoryItems[indexPath.item]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
return cell
}
else if collectionView == self.collectionViewHour {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell
cell.hourItem.text = self.hourItems[indexPath.item]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
return cell
}
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: minuteIdentifier, for: indexPath as IndexPath) as! MinuteCell
cell.minuteItem.text = self.minuteItems[indexPath.item]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
return cell
}
}
Can I use switch statement to achieve the same effect?Make the code look a little more elegant?
I hope my code looks like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell
switch collectionView {
case self.collectionViewCategory:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell
cell.CategoryIcon.image = self.categoryItems[indexPath.item]
case self.collectionViewHour:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell
cell.hourItem.text = self.hourItems[indexPath.item]
default:
//do nothing.
}
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
return cell
}
But I got a lot of mistakes, and I do not know how to fix them.
You can try to assign each collectionView a unique tag like so
collectionView.tag = 1
and then use the tag as the identifier:
switch collectionView.tag {
case 0:
// some code
case 1:
// some code
default:
// some code
}
If you're using your collectionViews inside UITableViewCells you can set each collectionView.tag to be indexPath.section or indexPath.row within tableView(_ tableView: willDisplay cell: forRowAt indexPath:).
Here's a nice subclass you could use:
class CollectionViewTableViewCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
func setCollectionViewDataSourceDelegate(dataSource: UICollectionViewDataSource?, dataDelegate: UICollectionViewDelegate?, forSection section: Int) {
collectionView.delegate = dataSource as! UICollectionViewDelegate?
collectionView.dataSource = dataDelegate as! UICollectionViewDataSource?
collectionView.tag = section
collectionView.reloadData()
}
}
and then in your view controller implement
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? CollectionViewTableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(dataSource: self, dataDelegate: self, forSection: indexPath.section)
}
Hope this help. Not testing at all. If any issues, can you take a snip ?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell
switch collectionView {
case self.collectionViewCategory:
let categoryCell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryIdentifier, for: indexPath as IndexPath) as! CategoryCell
categoryCell.CategoryIcon.image = self.categoryItems[indexPath.item]
cell = categoryCell
case self.collectionViewHour:
let hourCell = collectionView.dequeueReusableCell(withReuseIdentifier: hourIdentifier, for: indexPath as IndexPath) as! HourCell
hourCell.hourItem.text = self.hourItems[indexPath.item]
cell = hourCell
default:
//do nothing.
}
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
return cell
}

Swift: How to reload row height in UITableViewCell without reloading data

I have a case in which I have to reload only height of a UITableViewCell.
but if I call the function
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
it reloads the height as well as the data of the cell.
How can i just control the height of cell in Swift?
This is my cellForRow block :
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
cell.heading.text = headerText
return cell
}
else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! CustomTableViewCell2
ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in
cell.mainImage.image = image
})
return cell
}
else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as! CustomTableViewCell3
//cell.aurthorImage.image = UIImage(named : "obama")
ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in
cell.aurthorImage.image = image
})
cell.aurthorImage.tag = aurthorID
cell.aurthorImage.layer.cornerRadius = cell.aurthorImage.frame.height/2
cell.aurthorImage.clipsToBounds = true
cell.aurthorImage.userInteractionEnabled = true
cell.aurthorImage.addGestureRecognizer(aurthorImageTapRecignizer)
cell.aurthorName.text = self.authorName
cell.time.text = self.time
self.followButton = cell.followButton
return cell
}
else if indexPath.row == 3 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell4", forIndexPath: indexPath) as! CustomTableViewCell4
let htmlHeight = contentHeights[indexPath.row]
cell.webElement.tag = indexPath.row
cell.webElement.delegate = self
cell.webElement.loadHTMLString(HTMLContent, baseURL: nil)
cell.webElement.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
return cell
}
else if indexPath.row == 4 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
cell.heading.text = "Related Posts"
return cell
}
else if indexPath.row == 5{
let cell = tableView.dequeueReusableCellWithIdentifier("cell6", forIndexPath: indexPath) as! CustomTableViewCell6
return cell
}
else if indexPath.row == 6 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
cell.heading.text = "Comments"
return cell
}
else if indexPath.row == 7 {
let cell = tableView.dequeueReusableCellWithIdentifier("cell5", forIndexPath: indexPath) as! CustomTableViewCell5
let htmlHeight = contentHeights[indexPath.row]
self.commentSection = cell.commentsView
self.commentSection.tag = indexPath.row
self.commentSection.delegate = self
let url = NSURL(string: commentsURL)
let requestObj = NSURLRequest(URL: url! )
self.commentSection.loadRequest(requestObj)
self.commentSection.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
commentSectionDidNotLoad = false
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1
cell.heading.text = headerText
return cell
}
You can use this code to update the cell's height without reloading their data:
tableView.beginUpdates()
tableView.endUpdates()
You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.
UITableView Class Reference
start an update of the tableview and then end it without changing anything.
tableView.beginUpdates()
tableView.endUpdates()
This should make the tableview set the new height
You can regulate the height by either implementing the (a)heightForRowAtIndexPath with logic setting the heights or (b)with auto layout and automatic tableview row height
A.
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if [your condition, row == 5 in your comment] {
return 100
} else {
return 40
}
}
Whenever you want to change the height you would just call these two rows
tableView.beginUpdates()
tableView.endUpdates()
B.
in viewDidLoad
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
and then you can just expose the layout constraint that set's the cells height and access it to set the height when you want
cell.heightConstraint.constant = 100
tableView.beginUpdates()
tableView.endUpdates()
About your question for example, to give a specific height dimension :
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 3 {
return 50.0
}
return 72.0
}
But I think you have a webView inside a cell so, generally, to calculate the dynamic height of a UITableViewCell with a UIWebView:
(this example have two webViews)
class MyTableViewController: UITableViewController, UIWebViewDelegate
{
var content : [String] = ["<!DOCTYPE html><html><head><title>Page Title</title></head><body><h1>My First Heading</h1><p>My first paragraph</p></body></html>", "<HTML><HEAD><TITLE>Coca-Cola</TITLE></HEAD><BODY>In Chinese, Coca-Cola means Bite the Wax Tadpole</BODY></HTML>"]
var contentHeights : [CGFloat] = [0.0, 0.0]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCell", forIndexPath: indexPath) as! MyCustomCell
let htmlString = content[indexPath.row]
let htmlHeight = contentHeights[indexPath.row]
cell.webView.tag = indexPath.row
cell.webView.delegate = self
cell.webView.loadHTMLString(htmlString, baseURL: nil)
cell.webView.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return contentHeights[indexPath.row]
}
func webViewDidFinishLoad(webView: UIWebView)
{
if (contentHeights[webView.tag] != 0.0)
{
// height knowed, no need to reload cell
return
}
contentHeights[webView.tag] = webView.scrollView.contentSize.height
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
}
}
You can use the view height constraint in the cell, by updating the view in cell height you can update the specific cell height.
let height = cell.Height_constraint.constant
cell.Height_constraint.constant = height + 200 //200 you can use any number
Height_constraint: is the height constraint of subView in my cell.

TableView With 3 Sections and cells crashing

I have a simple UITableViewController with 3 sections and each section has 3 rows. I have already allocated each cell it's reuse identifier.
When I run the code, I get an EXE_BAD_INSTRUCTION error at return cell.
Here is my code:
import UIKit
class settingsViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell!
if indexPath.section == 1
{
if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("draftCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("logoutCell", forIndexPath: indexPath) as UITableViewCell
}
}
else if indexPath.section == 2{
if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("facebookCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("twitterCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("rateusCell", forIndexPath: indexPath) as UITableViewCell
}
}
else if indexPath.section == 3
{
if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("creditsCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("contactusCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("termsandconditionsCell", forIndexPath: indexPath) as UITableViewCell
}
}
return cell
}
For section number 0 you haven't written cellForRowAtIndexPath. You have given number of sections = 3, so its index starts from 0,
Correct your indexpath.section comparison i.e do it for indexpath.section == 0, indexpath.section == 1 and indexpath.section == 2.
Also you haven't registered class for tableview cell like
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
You would be better off using a static tableView in storyboard, instead of trying to programmatically code static cells in an dynamic tableView.
Apple's Table View Programming Guide recommends:
Use static cells to design a table with a fixed number of rows, each with its own layout. Use static cells when you know what the table looks like at design time, regardless of the specific information it displays.
If you are determined to dynamically handle this, your code would be easier to read and maintain if you used a switch statement, instead of nested ifs.
You have 3 sections but the index should start from 0 (0,1,2,3,...). So modify your code starting from indexPath.section == 0 (which is the first section of your table). Furthermore I agree with PetahChristian that you may use static cell to design your table.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell!
if indexPath.section == 0
{
if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("draftCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("logoutCell", forIndexPath: indexPath) as UITableViewCell
}
}
else if indexPath.section == 1{
if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("facebookCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("twitterCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("rateusCell", forIndexPath: indexPath) as UITableViewCell
}
}
else if indexPath.section == 2
{
if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("creditsCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 1
{
cell = tableView.dequeueReusableCellWithIdentifier("contactusCell", forIndexPath: indexPath) as UITableViewCell
}
else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("termsandconditionsCell", forIndexPath: indexPath) as UITableViewCell
}
}
return cell
}