swift - there is a cell in the empty tableview - swift

I have several tableviews, one of them uses coredata and others get data from a json api. In my tableview which gets data from coredata, when I launch the app there is a cell, but my coredata is empty. Cell disappears when I reload the table.
How am I going to remove that cell?

You could simply add code to erase all cells if your core data is empty. You do this in the ViewWillAppear(), if your core data object is empty then return 0 at your numberOfRows in table.
You could also add a self update function (by NSNotifications) which reads the current amount of data (count) and then do a reloadData() call.
These are only suggestions... we don't really know how your code looks like to be able to suggest a better answer.
Cheers

You can simply add the following line of code in viewDidLoad() or viewWillAppear() to clear unwanted cells from UITableview.
tableView.tableFooterView = UIView(frame: CGRectZero)
If Table is initially empty then there will be no cell to display.

Related

Discard changes inside UITableView do not reflect on the cell with UITextView that is not visible but active

I have UITableView with UITableViewCells that contains UITextView. And I have logic for temporary data and saved data. So, when user edit that cells everything is working, including discarding changes that I do with swapping temporary data and saved data. Then I do reloadData on table view and everything is showed correctly.
My problem is when the user clicks on the textfield and scrolls the table view until the "active" text view cell is not visible and the changes are then discarded. What happens is that all is discarded, but the active cell has the latest data that is not discarded. If I put "cursor" on some other cell and then discard changes again, it'll be ok, all cells that aren't active will discard data.
So, discarding data is not working only on cell that is not visible but still has active uitextview.
I tried to get "latest" active cell and manually reload them but it's not working. I have tried to resignFirstResponder() from tableview and then discarding data and reloading tableview but it's not working.
I expect all text views inside the tableview to have old data that is not saved when I click discard changes.
Cell that is still active (with an active text view) and is not visible on the screen won't discard changes, it will still have old data (but it will not be active anymore)
EDIT:
Here is how everything is working.
Here is how my code is working (very simplified).
So, I have
ViewModel -> ViewController -> Cell
On viewModel i have:
var data = ["Some", "things", "are", "good"]
tempData = []
on init I put tempData = data
(we are here talking about structs so value types, and everything is working as I said regarding that)
on cellForRow I have
cell.data = viewModel.data[indexPath.row]
and If I click discard changes on viewController I just do this in viewModel:
data = tempData
and on ViewController
tableView.reloadData()
and everything is working.
Only thing that is not working is that active cell that is not on the screen. It's not affected by changes in "core" dataset.
So maybe there's a connection about UITableView can't refresh cells that have active UITextView and are not visible at the moment of reloadData()? I don't know about that.
EDIT2:
cellForRow
guard let cell: ExampleTableViewCell = tableView.dequeueReusableCell(),
let data = viewModel.data[safe: indexPath.row] else {
return UITableViewCell()
}
cell.model = (name: data.name, surname: data.surname)
cell.onTextViewDidChange = { [weak self] text in
self?.tableView.beginUpdates()
self?.tableView.endUpdates()
UIView.setAnimationsEnabled(true)
self?.viewModel.update(text: text, index: indexPath.row)
}
return cell
One of the key features of UITableView is that it reuses the same instances of the cells over and over again, only keeping as many unique instances as it takes to simulate there being infinite instances. Notice how in tableView(_: cellForRowAt: ) you do not initialize a cell, you dequeue it. This is because UIKit manages when the cells are initialized for the first time and when they are "recycled", hence RecyclerView on Android.
Without being able to look at your code, I would guess that the issue is that your temporary data is stored inside of an instance of the cell and when the cell scrolls offscreen, something is happening in your tableView(_: cellForRowAt) function which tells the cell to display a different cell's data and doesn't save the temporary data. My suggestion for a fix if this is the case would be to store the temporary data in your UIViewController rather than in the cell. It is also better MVC to store this kind of data in the controller rather than in the view.

Swift problems with loading data into UITableView using dictionary keys and values

Im a beginner to developing apps and I'm trying to create my first real app using Xcode/Swift.
The problem I am having is setting up a table view to show certain information. I've managed to create other table views successfully but this one is slightly more complicated. I'll start off by breaking it down.
I have a variable that receives data via a segue called var recievedSelection = (choice: "", choiceValue:0.0) 'choice' being the name of selection and 'choiceValue' being the cost of that selection.
I then store this data into a dictionary array called var dict:[String:Double] = ["":0.0] by using this line of code:
self.dict[self.recievedSelection.choice] = self.recievedSelection.choiceValue
I have a table view all set up with delegate methods etc working and dequeueReusableCellWithIdentifier("basic", forIndexPath: indexPath). The numberOfSectionInTableView return 1 and numberOfRowsInSection return self.dict.count
What I am having problems with is using the data of each 'key' of self.dict to set as the cell.textLabel?.text and the data of each 'value' of self.dict to set as the cell.detailTextLabel?.text so that when the tableView loads the name and price of that selection is visible in each cell. Once user has made all their selections/choices as there will be a few to make throughout the app, there should be a list of those selections/choices that can be viewed in this table view.
Then there is a "Total" amount at the bottom just under the table view that calculates the overall amount.
I have also set up the table view to delete individual cells that when and if a cell is deleted that amount/price set into that cell is deducted from the total at the bottom of screen.
I am using NSUserDefaults to save the data loaded into the table view as there isn't any personal data being stored and it is only a brief estimate.
But the problem is, I can't get the table view to load both the values and keys of 'self.dict' into the table view textLabel and the detailTextLabel?
I hope that makes sense! I'd really appreciate any help.
I'm going to assume that you want to get values for your UITableViewDataSource from a Dictionary. An Array would be better, and you can likely change your code to use one for the UIViewController's data, but if you must, first get a set of the Dictionary's keys into an Array:
var myDataSource = Array(self.dict.keys)
Reassign this value EVERY TIME you edit/delete from the Dictionary, before you call tableView.reloadData(), to avoid index out of bounds crashes. Now, in your dataSource methods, you can check
self.dict[myDataSource[indexPath.row]]
and
myDataSource[indexPath.row]
and
return self.dict.count
This will give you both the size of the array that you need for number of rows, and the value and key you need from your Dictionary. If you like, you can also iterate over a Dictionary's keys and values with:
for (key, value) in self.dict {
//do something
}
I am not completely sure from your question what is causing the problem, meaning i'm not sure which value you are not getting. However, you can get those values by using:
let keyArray = dict.allKeys
then in your cellForIndexPath:
cell.textLabel.text? = keyArray[indexPath.row] //the "key"'s Value
cell.detailtextLabel.text? = dict[keyArray[indexPath.row]] // the "value"s value
Hope this helps your issue?

How To Set Nav Bar Item Title From plist's index row?

My data is sourced in a plist and displayed in a tableview with navigation bar.
I want to load the subview with the cell that was clicked.
I know this would set it correct, but I'm not sure how to implement indexPath in the viewDidLoad method?
self.navigationItem.title = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey: #"name"];
You have an array of Dictionary With you. Now IndexPath is seen called in TableView delegate methods. I doesn't mean that you can't create one. Create an NSIndexpath obect and assign the indexes parameters and send it to the function which contains the above code( separate the code which sets the title from the Cell for row at index path).
But the problem is as I see, You want to show only a specific set of data in dictionary which is stored in an Array. The real problem is You should know which the index of the Array you want to load. to the title and the tableview. If you can know that then its solved.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html
Regards ,
Jackson Sunny Rodrigues

iPhone UITableView - Visible Cells

I have a grouped UITableView, with cells in section 2 depending on cells in section 1. More precisely, each cell in section 1 is associated with multiple cells of section two and deleting a row in section 1 needs to delete the associated rows in section 2.
I have my dataSources all set up and everything works fine if all cells are visible. However, if the cells from section 2 haven't been loaded in the UITableView yet, I have a problem because the data source is updated for section 2 too.
I'm looking at this method visibleCells in UITableView. But I'm using custom UITableView cells and get an unrecognized selector exception if I try to access one of the labels in a cell.
How do I get around this?
Thanks,
Teja.
I believe what you were looking for is:
if ([tableView visibleCells] containsObject: theCellOfInterest]) {
// Do whatever you want to do.
}
Sorry if I wasn't clear the first time (or maybe even the second time too), but here's an answer to a repost of the same question.
Deleting multiple (not yet loaded) rows in UITableView

How do I set a custom cell for non databound TableCells

I am happily able to set the styling of my tableviewcells using the cellForRowAtIndexPath delegate, but I want to set the background of all the nonpopulated cells which are still on the screen
hey, basically any cell which is part of your tableview belongs to a particular index and particular row
so incase you are not populating a particular section via data source - you can still get a reference to a cell in that indexPath by manually calling the cellForRowAtIndexPath delegate, and passing the section as well as row index
it returns you a cell. assign it to your private variable and edit it the way you want to.
Use the visibleCells method on your table view. Then do what you wish to the cells.
Alternately, you could call indexPathsForVisibleRows to get just the index paths. Then call cellForRowAtIndexPath: to get the cell corresponding to each index path.