I tried to add many details (fields) to display in TableView. by concat each fields and put in for detailTextLabel e.g.:
From:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
cell.textLabel.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel.text = taskMgr.tasks[indexPath.row].desc
return cell
}
To
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Test")
cell.textLabel?.text=tasksList[indexPath.row].name
var yString = tasksList[indexPath.row].desc
var xString = tasksList[indexPath.row].amnt
var zString = xString+" "+yString
cell.detailTextLabel?.text=zString
return cell
}
Could anyone point out any other ways for better code for many fields to display in the view.
I think your first method is great. In storyboard inside a prototype cell you can place several UILabels. Then just update each label as you did. You can place labels in a xib or create them in code. But there is nothing wrong with having several labels and then updating them individually. This gives more flexibility that concatenation because you can justify the text in each label as required to make the tableview pretty. So each cell row has good alignment with the one above it.
Related
I created two ViewControllers and two TableViews. Then i added prototype cell to one TableView, set it up according to my needs, copied it to the other TableView, changed its class and identifier and linked it up in ViewController that is datasource and delegate for each one.
The problem is, FEEDING one is behaving good, having constraints as expected, and the WALKING one is not, but i have no idea why since they have all same properties in each one's:
ViewControllers:
FEEDING
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myFeedingTableView.dequeueReusableCellWithIdentifier("feedingcell", forIndexPath: indexPath) as! FeedingCell
cell.time.text = self.vremena[indexPath.row]
return cell
}
WALKING
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myWalkingTableView.dequeueReusableCellWithIdentifier("walkingcell", forIndexPath: indexPath) as! WalkingCell
cell.time.text = self.vremena[indexPath.row]
return cell
}
CustomCell files
each one is connected to its class
FeedingCell is class of feeding prototype cell
WalkingCell is class of feeding prototype cell
Constraints
and the constraints are same, as you can see on the picture.
Here is the image providing different results and constraints:
image
Solved by changing rowHeight settings in TableView. Thanks #SilentLupin
I have to show 2 different cells in a table. I have tried it by setting a prototype to a table. but it is still showing Prototype table cells must have reuse identifiers warning.
could someone please guide me to resolve this warning.
Followed this link:
UITableview with more than One Custom Cells with Swift
In storyboard you have to define the Identifier for the cells like the below image
Then in cellForRowAtIndexPath you have to use the specific identifier for specific cell like this
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier1")
//set the data here
return cell
}
else if indexPath.row == 1 {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier2")
//set the data here
return cell
}
}
You must set the Reuse Identifier for both prototype cells, and they must be different. Then in your cellForItemAtIndexPath method, you must dequeue the cells using the corresponding Reuse Identifier based on the indexPath given.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableView {
switch indexPath.section {
case 0:
return tableView.dequeueReusableCellWithIdentifier("CustomCell1", forIndexPath: indexPath)
case 1:
return tableView.dequeueReusableCellWithIdentifier("CustomCell2", forIndexPath: indexPath)
break:
return UITableViewCell()
}
}
This is what my story board shows
However, the right arrow is not there when I run on simulator
I don't know why and I don't know what code I should show you because I actually didn't make that arrow myself, I just selected the cell's accessory as Discolosure Indicator
update 1
there is no overlap, this is from the debugger:
The problem is that you implemented layoutSubviews, but never called super.layoutSubviews(). Thus the disclosure indicator was never laid out.
Try adding this inside your tableview method
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
//Complete Code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
try adding disclosure indicator programmatically in cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
...
}
Try using preview in the assistant editor. Most likely one of the images is overlapping the indicator and you need to adjust your constraints.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
cannot invoke 'dequeReusable...' with an argument of type '(String)'
on line var cell:UITableViewCell =
self.tableView.dequeueReusableCellWithIdentifier("cell") as
UITableViewCell
(sorry i'm extremely new to Xcode and swift)
when i click my UITableView in my storyboard, it is a table view cell with style Subtitle and identifier cell. (I may be doing this completely wrong, not sure)
I think I find the problem, try to take the word self of the dequeueReusableCellWithIdentifier call, like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
The reason been that you want to use the tableView coming from the function and not the tableView that belongs to the class.
Let me know if this solve your problem
Please define your cell identifier in storyboard.
I have this code, that I copied out of the default MasterDetail App, yet when I run the program, it stalls on the line with the declaration of the cell. And I have no clue why.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = objects[indexPath.row].title as String
cell.textLabel.text = object
return cell
}
You haven't register cell to use on table view
I hope you forgot to do this.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
If you didn't register the cell for tableview, then it may return nil to cell.
Apple Says
let == Constant
var == variable values at any time
So using ? will consider as optional
you should have like this
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
//OR
//var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
//Your stuff
return cell
}
According to This tutorial from Apple, you may want to check the Table View on the story board.
Look for the attribute inspector for the Table View and verify that the "content" field is set to "Dynamic Prototypes". Updating this fixed it for me.
Also, while you have the hood open, verify that there is a single table cell in the table view, and make sure that it's "Identifier" field is set to the value you had in the tableView.dequeueReusableCellWithIdentifier method call (in your case "cell")