tableview, adding a row, iphone app, objective c, - iphone

I have a tableview set up that i can drill down about 3 levels.
I have an edit button at the top that lets me delete rows.
I've just used a mutable array to store data and I can remove items from the array.
This all works fine.
For the life of me I can't figure out how to add a row.
I've been through all the documentation but can't get it working.
I've tried countless things.
I've edited numberOfRowsInSection so that in editing mode it is incremented by 1. But I don't know how to make that row appear.
I want an extra row at the bottom (in editing mode) so that I can add a row.
I just don't know how to get that row on the screen.
If someone has code for a simple tableview, just one view, that has an edit button that allows for adding and deleting a row I'd really appreciate it.
Just can't get my head around something here.
Cheers.
Stephen

You can add new item to array and call [UITableView reloadData].
Or you can use:
[tableView beginUpdate];
[tableView insertRowsAtIndexPaths:*arrayOfIndexPaths* withRowAnimation:*rowAnimation*];
[tableView endUpdate];

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 reload a UITableView for more data?

I have UITableView, which I fill from a JSON Query.
I fill it with Private Messages and it works.
The JSON Query has the properties of "site" in which I get for every site 20 PMs. So at the beginning I got Site=0 and got 20PMS which are loaded.
Now I wanna have the features like in the Email-app (i believe I saw it there): When you scroll down and reach the end (in my app reached the 20. PM), the application should load the next 20 and so on and so on.
Any ideas how to realize?
Save the index path of your last row and implement
[UITableView reloadRowsAtIndexPaths:withRowAnimation:]
Or you can use scrollToRowAtIndexPath:atScrollPosition:animated:
scrollPosition
A constant that identifies a relative position in the receiving table view
(top, middle, bottom) for row when scrolling concludes.
See “Table View Scroll Position” a descriptions of valid constants in docs
And once you get you last scroll position just reload your table view [tableView reloadData]
But there is problem i guess once you reload the data i think after reloading(I am not sure) tableView it will take you to first row again then you can always save the lastIndexpath of that tableView and after reloadData just scrollToRowAtIndexPath:atScrollPosition:animated: or you can use scrollToNearestSelectedRowAtScrollPosition:animated:
I think you can implement feature like email app using your custom logic. What you do is keep the last row of tableview for loading more data. There should be message like "Load more..." in that last row. Whenever user touches that row, you should load more data and update tableview.
Stephen James and his colleagues at Key Options Development came across a similar problem: http://blog.keyoptions.co/tableview-with-automatic-load-more
They have built a sample app (ContinousTableview) that should be of some help to you. The source code is available at GitHub: https://github.com/stephenjames/ContinuousTableview

Help Using UITableView for High Score Page

I am making a simple gaming app, implementing a tab bar controller. The second tab, or high score page is what I am having problems with. I am able to populate the UITableView with an initial array of objects, however I can't seem to add new cells. Now I read about User entered cells, but how exactly would I extract the winner from one tab to populate the UITableView in the high score tab? Thank you for your time!
For adding new cells to your UITableView, the most straightforward way would be by simply reloading the entire table view [UITableView reloadData].
Assuming that you are storing the cell-contents in a mutable array, simply add those new entries to that array and reload the table.
You can update your array and call reloadData on table view to reload it and also if you want to add cell at a specific position you can use
(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:

iphone Table View Issue

i have two arrays one for search bar and other for contact array. and first one display as tableview. the problem is that when i select two contacts from contact list and then i try to go on my search bar the selected contacts gone as shown in figure 2.
i try to reload table data but it didnot work. please help me... tha nks in advance.
You have a issue with the cells my best guess you have to do something like this before acctually drawing the cell in cellForRowAtIndexPath:
cell.clearsContextBeforeDrawing = YES;
If that doesn't work it's probably a problem with the indexPath, could you provide some more information related to the code?
If your cells are being released or redrawn at any point, they won't remember they were selected unless you are storing this data somewhere. The cells will be redrawn to their original state OR potentially reused if you are enabling this. I would track your selected cells alongside your contacts array (maybe an NSDictionary?).

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.