as you know tableViewCells can be displayed incorrectly after scrolling up and down, if you dont reset them in a certain way. After searching the internet without finding a satisfying answer, I would like to know how to do exactly that.
I am using one prototypeCell with an identifier that is reused. The titles and subtitles are currently stored in arrays. Depending on the indexpath, the specific string is taken from them. But how do i reset the content in a way, that the correct String is displayed? At the moment, the "incorrect" cell shows the title of the cell last initialised.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = cellNames[row]
cell.detailTextLabel?.text = cellDetails[row]
return cell
Thank you for answering
In your cellForRowAtIndexPath: delegate method, reset ALL the modified properties of the cell before returning it. That's 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
Basically I'm building an iOS app which allows users to track their moods throughout the month so that they can evaluate when they are most stressed and when they are most happy.
Each day, users can write a block of information about the day and colour code each day so that in a list view they can quickly see how the month is going (for example, if most of the month is red then it indicates that they are having a bad month).
I am using green, amber, and red to identify the moods of users. But I'm having trouble making each individual cell the colour that the user has selected. I have given each colour cell a tag to identify the user's choice. I'm just having trouble actually calling the function to allow it to be displayed on the screen.
This is the code in my list view controller:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
let post = posts[indexPath.row]
let cellTitle = cell.viewWithTag(1) as! UILabel
cellTitle.text = post.title
let cellText = cell.viewWithTag(2) as! UILabel
cellText.text = post.text
return cell
}
I know that I need to call the functions, but how do I do this?
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.greenColor() // or any other color depending on the tag
}
If I understood you correctly you want to modify the color of specific cell in the table view. The simplest solution is to modify the cell in your function which is required for the table view anyways.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
After you specify which cell you want to edit using indexPath use cell.backgroundColor = UIColor.yourColor() to change the background.
I have a cell with one UISegmentedControl as shown below
i know the code below is very wrong or its not a good logic
//UIViewController
var globalCell = segmentTblCell() //CUSTOM UITableViewCell Class
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = segTblVw.dequeueReusableCellWithIdentifier("segment", forIndexPath: indexPath) as! segmentTblCell
globalCell = segTblVw.dequeueReusableCellWithIdentifier("segment", forIndexPath: indexPath) as! segmentTblCell //THIS LINE GIVES VERY STRANGE OUTPUT
return cell
}
to my knowledge, that line is only creating new object of cells when invoking cellForRowAtIndexPath, it will create more memory issues..
but what is the speciality in that line for remembering segment selected index corresponding with cell indexPath?
and i tried,
in cellForRowAtIndexPath, put a NSLOG for printing address of globalCell, address of cell & indexPath.row , and on scrolling, cell is reusing, globalCell will create new objects
by adding UISwitch to my cell, output is same
scroll UITableView many times, it makes very slow because every time THAT LINE will create many cell
by adding UITextField to my cell, then i found it affect the segment selected index
Can anyone tell me the reason for this???
Reason for asking this question is, McDonal_11's answer in UISegment value changing when tableview get scrolled
regarding to my knowledge, that line is only creating new object of cells when invoking cellForRowAtIndexPath, it will create more memory issues.. but what is the speciality in that line for rememebering segment selected index?
the cell is only created when there are none in the pool. otherwise, you'll get one that was created before. what you'll want to do is set the cell state each time in cellForRowAtIndexPath
for example
let cellIdentifier = "Filler"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:indexPath)
cell.propertyForSegIndex = 1 // or whatever i want it to be
This line:
globalCell = segTblVw.dequeueReusableCellWithIdentifier("segment", forIndexPath: indexPath) as! segmentTblCell //THIS LINE GIVES VERY STRANGE OUTPUT
Makes no sense, and should not be there. Get rid of it.
Here's the deal: When you call dequeueReusableCellWithIdentifier, you get back a cell that was probably used to display other data. It's views will have old values in them. In your case, you'll get cells that have their segmented control set to values other than the first segment.
When you fetch a recycled cell, you always have to fully configure it. Assume all views have old values. (Assume image views are not empty - they contain some other image. Labels contain the wrong text. Segmented controls have the wrong segment selected, etc.)
I have a UITableView with prototype cells that essentially takes up the whole page. I have a UIButton on the very bottom that should display a pop-up static UITableView when tapped. I'm struggling to account for the pop-up table view in cellForRowAtIndexPath.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let result: UITableViewCell
if tableView == self.tableView {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = array[indexPath.row]
result = cell
} else if tableView == self.popUpTableView {
var popUpCell = self.popUpTableView.dequeueReusableCellWithIdentifier("popUpCell", forIndexPath: indexPath) as! UITableViewCell
popUpCell.textLabel!.text = popUpArray[indexPath.row]
result = popUpCell
}
return result
}
I'm getting an error at return result, where Variable 'result' used before being initialized, but I'm declaring it at the very top. Where am I going wrong with this?
You need to have exhaustive choices. It's possible that result never gets initialized because your checks are "if" and "else if". What happens if tableView is not either "self.tableView" or "self.popUpTableView"?
The simple fix is (if you only plan on having these two) to simply change your "else if" to a simple "else". This way result will always get initialized.
I need to test if the value of a custom cell label is contained within an array. To achieve this I created a function that does the test and returns a boolean. My problem is that every time the cell enters the screen, the test is done again.
Where should I make the test so it is done only once ?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell
var name:String = Array(self.contactList[indexPath.row].keys)[0]
var phones:String = self.contactList[indexPath.row]["\(name)"]!
cell.nameLabel.text = name
cell.phoneLabel.text = phones
cell.inviteButton.tag = indexPath.row
var (matched, id) = self.isRegistered(phones)
if(matched){
cell.phoneLabel.text = "TRUE"
cell.idUser = "\(id)"
}
return cell
}
I need to be able to modify the cell in response to this test. Thanks for your help !
Your test is called every time the cell appears because you a placed the test call inside cellForRowAtIndexPath: (If like you said you need to change the cell's label value according to the test result, I don't see any other choice)
You can try using the UITableViewDelegate methods
optional func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
This will be called every time a cell needs to be presented.
Second way is a bit tricky and I don't recommend it, you can create a pool (array) of UITableViewCells and maintain (call test & change values) from that pool at a time you think is best (when scrolling the tableView for example)