STOP the tableview , arrays, labels, from reset data automatically in TodayWidget extension? - swift

I'm using xcode 7 swift 2
in my app extension(TodayWidget extension) i'v put 2 table views that store the calculating results.......
2 TableViews
and its work fine, even if i scroll TodayWidget/notification view UP and DOWN again the data is there
Data
BUT sometimes after i scroll TodayWidget/notification view UP and DOWN again all data is lost and reset values to 0 again !!???
how i can prevent that from happening .....!!!

The table view have to reset the data in table view cells automatically. that's how it works. while scrolling, as soon as the cell becomes invisible it will be taked to be reused for other indexpath.
To achieve what you want in quite simple. You just need to have a dataSource to save your data in so you will not lose it while scrolling.
If you share some of your code I'll be able to give you more details how to solve this problem.

Related

Custom Cell with Button Memory Problems

I have a custom cell that contains a button in a table view. The button is used as a toggle to essentially serve as a "checkbox" for a user to check off certain items in the list. I was having the issue in which the buttons in these table cells seemed to be sharing memory locations as a result of the dequeuereusablecellwithidentifier. When a button was pressed, it would also press every 4th or 5th button in the list.
I changed it to create my cells in a method into an array which then populates the tableview. This works fine for what I am trying to achieve, however it poses an issue when dealing with large row counts. The tableview itself runs quickly, but the initial load can be 3-4 seconds at times when there are over 100 rows. The iteration to create the cells and then populate it to the tableview is quite cumbersome.
What other methods can you populate a tableview with custom cells and buttons while still retaining unique memory for the buttons within?
Any help would be appreciated!
Thanks :)
You definitely don't want to change the way the creation of cells work- dequeuereusablecellwithidentifier is a very good thing for the reasons your seeing.
The solution is that you should store the result of the button/checkbox press in a separate data structure, like an NSArray full of NSNumber. As your table scrolls and cells are reused, you reset the state of the checkbox to whatever state it should be based on your NSArray.
Good luck!

how to persist tableview data on vertical scroll

i have created scrollview and have loaded tableview on that programmatically .also i have created customize tableview cell and displaying data in tableview.But my problem is whenever i am scrolling tableview vertically my data is not getting persist and data of cell is getting erase.Please help me how shuld i store that data
It sounds as if you might not be using the reuseidentifiers of the table view properly. If you give all cells the same reuseidentifier then as you scroll the cells that go off screen might end up losing the customizations you made to them. I suggest getting a good understanding of reuseidentifiers, you could start here:iphone-what-are-reuseidentifiers-uitableviewcell

How to remain the old visible cell's position after inserting new cells at the top of the UITableView

Suppose I have a table view, and I want to implement something like this:
The table view contains n cells by default.
New cells will be added at the top(head) of the table view.
The data source of the table view is a mutable array.
The problem is when I use [tableview reloadData], the new data is always shown at the top of the tableview, but what I want is remain the old visible cells at the old position, means no refresh after reload data. I had tried some solutions, and I found out that if I added new cells at the tail, and update the tableview, the old visible cells will remain old position without any extra effort. But I don't know how to remain the old visible cells at the old position if I add the new cells at the top.
As a reference, I think the official Twitter app for iPhone just implemented what I want in the time line view, but I don't know how to archive it.
Guys, have any idea?
Thanks a lot.
-Tonny Xu
[Update] It's a little bit hard to describe what I want in text. I added some pictures.
As the picture shows[the link is below], the default cells is started from section California, I want to added 3 new cells before "Brea", what I want is after I added "New cell 1,2,3", the cell "Brea" is still remain the position where it was. In another word, the new cells 1,2,3 are not visible after updated.
Sorry, because I don't have enough reputation to use image, please visit this url http://i.stack.imgur.com/S9jJl.png
I had figured out how to implement this, hope this can help somebody else who wants the same effect.
The point is that UITableView is a subclass of UIScrollView. Thus, UITableView has all the properties of UIScrollView, especially one will work for this: UITableView.contentOffset, this can also be animated using [UITableView setContentOffset:animated:].
To archive the same effect as Twitter for iPhone official app, we need to know every time how much offset is added or deleted. Remember the former offset and set the offset +/- delta offset without animation.
Done.
Just answered a similar question with code snippets here
Keep uitableview static when inserting rows at the top
Basically:
Save the scroll offset where you would have called beginUpdate:.
In place of calls to InsertCell you add the cell's height to the saved offset.
Where you would have called endUpdate:, call reloadData, then scroll by the saved offset with NO animation.

iPhone Dev: Get more data functionality in twitter iPhone clients?

I'm building an app (not necessarily a twitter client) and I'm trying to figure out how developers create the buttons above and below a table view where a user presses them to either reload newer data or reload older data into a table view. Does anyone know of any tutorials out there that does this or know of an easy way?
If you want fixed buttons, you can just make your table view not use the full screen and add the buttons in the space. If you want the buttons to scroll with the table view, you can add a header or footer view to the table and put your buttons inside that.
Check the Three20 project. I believe there's a tableview there that does that.
It's actually not that hard to add inline buttons to a tableview. First you check and see if there's actually more data to show. If so, you want to add 1 to the number of rows returned from the datasource. When asked to draw that last row you return a cell that contains "Press for more" caption as well as a hidden spinner instead of the standard cell that shows your normal data.
If the user presses that last button the table view handler turns on the spinner then fires off a network event. Once the network request completes the data is processed and added to the same tableview datasource that was used to draw the first table.
Now all you have to do is tell the tableview to reload itself and the new data will show up. If you want to limit the amount of data shown you can prune out N number of items from the head of the datasource before redrawing so the memory-use stays manageable.

Is it possible to resize a UITableViewCell without using UITableViewDelegate?

I want to resize my table view cells from inside the cell instead of from the UITableViewDelegate. I am resizing them based on asynchronous content, so I can't size them in the delegate.
I tried setting self.frame inside the cell, but the table view was really unhappy about that. Cells were overlapping and all kinds of craziness was going on.
You simply have to use the table view to control height. You can tell the table a cell has altered by using the calls to remove and then re-add specific cells, so you don't have to reload the whole table - but the height has to be fetched using the delegate callback tableView:heightForRow:atIndexPath:
I don't see why this is not practical though. You can have any number of asynch systems running that update a central height cache held by the table view delegate - every time you create a cell you can assign it the delegate as a reference so it has a way to talk back to the table and let it know cells need reloading and what the new heights are.
If you think about it, the poor table view is a scroll view that has to manage all these separate cells and keep them together visually - so it's really unkind of a cell to go rogue and start altering frames without letting the table view know what is going on anyway. It's best to let the table drive and tell it what to do.
No you can not set the cell's size without using the UITableViewDelegate. Changing the size of the cell with actually change the size of the cell, but it will not change the offsets that the UITableView draws the cells with. Which will result in overlaps, and gaps all over the place.
Your friend is tableView:heightForRowAtIndexPath:, and it should be fast. If you override it, then the table view can no longer make the assumption that all rows are of the same height. And thus it must query all rows for their height each time it fetches cells to draw.
You should try to manage the cells contentView propertys frame, instead of the cells frame itself
heres a reference http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView