func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrayLength
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
return cell
}
And it prints the next error: "Previous definition of 'tableView' is here"(Definition conflicts with previous value). Why they conflicts with each other?
So we finally found the answer for this. First at all the whole code for the Problem. (The JSON things coming from SwiftyJson)
The problem here was that Orkhan forgot to use the UITableViewDataSource protocol in the class definition and did not set the tableView.dataSource na delegate to his own class. After doing so the problem is solved.
Related
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = companyTableView.dequeueReusableCell(withIdentifier: "cell")
}
I get this Error: "Initialization of immutable value 'cell' was never used; consider replacing with assignment to '_' or removing it."
I used "cell" as identifier for my TableViewCell. How can I fix it?
The error message really says it all: you're assigning a value to the constant cell but never use that value.
Also, the function tableView(:cellForRowAt:) is declared as returning a UITableViewCell instance, which you never do.
Fixing both problems leads to this code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
// do stuff with the cell
return cell
}
I try to display a simple cell with one label and an add item button. I can't get it to display correctly. I spent a lot of time but I can't find the solution.
This is the result:
There is no add item button and no correct row. I have just two items in coredata.
This is my code:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let data = items[indexPath.row] as Item
cell.textLabel?.text = data.body
return cell
}
What are the problems? Can anyone help me to display add item button, correct row count, and customize height of the cell correctly? Thanks in advanced.
First lets return the correct number of rows unto the table view.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
Next lets get it to output unto the prototype cells.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
Also lets call the unto the view controller the following so that it can run properly.
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
One last thing, your prototype's cell has the same identifier as the one in your code. In my code I would make sure that the identifier in the prototype cell is "Cell" since I called it in UITableViewCell(...reuseIdentifier: "Cell").
View picture to see where to add the identifier in your storyboard
I have strange behavior with UITableView on Storyboard today. I have created UITableView on Storyboard. After that I drag a PrototyleCell to this table and choose style is Basic. And I implement DataSource and Delegate on my ViewController. It show to simulator normal. But I can't tap to table for select a cell and didSelectCellAtIndexPath don't work too. In Storyboard I have checked selectionStyle. If I change style to another style, It work normally.
So my question is: this is a bug or it is a behavior of UITableView? And anyone can give some explanation for it.
Here is my code: Problem Cell Code. I can't select when use it but when I set another style of cell on storyboard everything will ok.
Thanks in advance
No its neither bug nor normal behavior, you must be doing some basic mistake...
Try following code with tableview and prototype cell in storyboard, Everthing will work.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var dataSource = ["Ajay","Ajay","Ajay","Ajay","Ajay","Ajay"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "cell")
}
cell!.textLabel?.text = dataSource[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
} }
Have you added the
tableView: cellForRowAtIndexPath delegate method. This is a required delegate you must implement.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return titleList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = ("\(titleList[indexPath.row]) - \(timeDayList[indexPath.row]):\(timeHourList[indexPath.row]):\(timeMinuteList[indexPath.row]):\(timeSecondList[indexPath.row])")
return cell
}
I'd like to put the day, hour, minute, second into a new line. Is it possible or should I create another row for it.
You should create a custom UITableViewCell and
Either add labels to it and stack them one under the other
Or have a UITextView and show them each in a new line
I would personally go with the first option.
Though I assigned value(self) to delegate/dataSource, reloadData doesn't work.
So, I set breakpoint, its dataSource hasn't the data.
How do I work reloadData? Here is my example code.
func getData(query: String) {
// ...(data processing part)
var parser: NSXMLParser! = NSXMLParser(data: data)
parser.delegate = self
parser.parse() // parsing works well.
self.testTableView.hidden = false // breakpoint 1
self.testTableView.dataSource = self
self.testTableView.delegate = self
self.testTableView.reloadData()
// breakpoint 2
insertTableWithConstraint() // set constraints
// init. dataSource
self.testTableView.dataSource = nil
}
I tried both "self.testTableView.~" and "testTableView.~", but It didn't work. Here is variable sets in my breakpoint
testTableView = (UITableView) 00000001360dee00 some = (UITableView) 0x00000001360dee00 ... _dataSource = (sunny.TestViewController *)0x135d34950 [0] (sunny.TestViewController) ...testDatas = (NSMutableArray) "4 values"
I append the my CellForRowAtIndex code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString
cell.detailTextLabel?.text = testData.objectAtIndex(indexPath.row).valueForKey("roadAddress") as NSString
NSLog("cell.textLabel.text : \(cell.textLabel.text)") // It was not shown in exec.
NSLog("cell.detailTextLabel.text : \(cell.detailTextLabel?.text)") // It was not shown in exec.
return cell as UITableViewCell
}
I forgot to append the numberOfRowsInSection code. So I append it.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testDatas.count
}
1) You shouldn't call self.testTableView.dataSource = nil. If your app crashes without it, it probably means you're trying to access your data without first checking it is not empty.
2) I assume that your NSXMLParser is used to populate your UITableView rows. NSXMLParser works with a delegate mechanism, so you should call testTableView.reloadData() only when you're done parsing, not immediately like you're doing right now. A good place to call it would be in the parserDidEndDocument delegate function.
3) In your func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell function, check that indexPath corresponds to something accessible in your testDatas variable (which I assume is built during XML parsing). This might be why your app is crashing right now.
4) Also, be careful to appropriately set the cell count in the tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int delegate function (probably using testDatas.count).
First of all don't set, tableview's dataSource & delegate to nil, set them to self.
As you said your numberOfRowsInSection returns exact count of the datasource object. Then try this:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString
return cell
}
Hope this helps.. :)