I have built an app which has the ability to delete cells from a table, but only if there are more than two cells.
If there are two or less cells, it only lets me select them. Any ideas?
Here's a video to visualize it: http://slavingia.com/etc/helpme.mov
If you have the datasource, in the delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
you can see how many items are in the source, if its less than 2 then you can return none in this method which wont allow editing, otherwise you can return delete style so you can
Related
I want to handle case when user deselect row (that already selected) by tap on this row. It is well known that tableView:willDeselectRowAtIndexPath: / tableView:didDeselectRowAtIndexPath: delegate methods not called in this case: they are called only if you tap on other, unselected yet row (my table view have single selection mode).
tableView:didSelectRowAtIndexPath: method also not called when I am deselecting row.
Is there is any easy solutions?
Update
The problem was in tableView:willSelectRowAtIndexPath: method, where I return nil in some cases, thats why tableView:didSelectRowAtIndexPath: didn't call. Thanks #Nekto for useful information and helping.
Apple docs say next about - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode).
That means that this method should be called unless your code has a bug or you haven't updated delegate of your table view or table is in editing mode.
One of the possible problems could be that you have incorrectly implemented - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath and are returning nil for the selected row. In that case didSelect... isn't called:
Return nil if you don’t want the row deselected.
Alternatively you can implement another table view delegate method: - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath:
Tells the delegate that the highlight was removed from the row at the specified index path.
See more about managing selections in table views here.
You can still use tableView:didSelectRowAtIndexPath, just use it like a check mark system would work.
The answer here explains how to use a checkmark. You can use this, or use the answer here.
I have such use case. Why you don't create a variable for lastSelectedRow(here I have problem if it was NSIndexPath and I used two integers) and on the method for selecting row just compare currentSelectedRow with lastSelectedRow.
I am contemplating on what is the recommended way to display a notice that there is no data for a specific section in a collectionView/TableView
One way is to create a special cell and to put that in instead of the data cells. That feels odd, since the "empty notice" cell doesn't correlate with the data, which means I would need to spread a lot of conditions in didSelectItem, configuring the cell, etc
Using https://github.com/dzenbot/DZNEmptyDataSet is appropriate only when the entire view is empty, not in a specific section
Another way would be (which is what I do now) to insert a UIView into the place where the data would be as a subview of the collectionview, But this also would require maintenance when reloading the data, scrolling, tapping. Also this requires calculation of where to place the view, which means I need to change it per collectionview, since it is not part of the collectionviewlayout
Is there a recommended pattern to deal with these situations?
Maybe you shouldn't have that section at all if the section contains empty data. Reduce section count by 1 at - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView datasource method and recheck the cell to be displayed during - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath datasource method.
In reply to you, I think you would have some type of "Add" button to add comments, photos, friends, etc. Make the option to add a comment or photo less obtrusive than taking an entire tableView row, and omit the comment or photo section, if there are no comments or photos.
If you put an "Add comment" or "Add photo" row while displaying details, users will likely get tired of unnecessarily reading that, as they use the app.
Note that tableViews generally handle insertion in editing mode, and when not editing, no section is shown if it is empty.
In my project I've got UITableView.
I need to assign different buttons, with different behaviours for different columns.
Question:
Where is the correct way to set different behaviours & actions for buttons? Is it in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath?
How is the correct way to assign different buttons with different behaviours to different columns? I am thinking about [_buttonArray objectAtIndex:indexPath.section]; is it the right way?
Example: Column 1 - Button A -> after button A pressed change button.title and size and do X.
You should use the below method for button behaviors if you want the user to be able to hit the entire cell and have the button action fire. If not, you should use an action delegate in your custom cell class. Let me know if you need help setting up the protocols file and or delegate method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
I want to load my data on scroll down for my tableView.
I have a JSON API from where I get my data.
Its a Private Message System.
Everytime I will get 20 entries, I can select a page (0 to x) get the first to x entries (everytime 20 entries).
So at the moment I have my table and I load a list of 20 entries and show them.
Now I want to have as soon as I reach the last entry it should load the next 20 entries.
I try to read many things about it here on StakcOVerflow and look at a Github Project with this but don't understand it at all.
Can someone tell me how to do this?
You can use the willDisplayCell method of UITableview. For ex, to see whether a row has 'load-more' button if you have one.
See this post.
A Github project.
you can implement this delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//put data on your array
[tableView reloadData];
}
I'm trying to create a button set up exactly like this:
How would I do this? Can someone show me the code? Is there a way to "Group" UIButtons like this?
You'll want to use a UITableViewController with the sections set to be UITableViewStyleGrouped.
Each of the groups is a section, so you'll want to return how many sections you have with - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.
For every section, you want to specify how many rows there are with - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section.
You'll want to customize each cell with - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath (which, by the way, tells you which section's particular row you're modifying in the indexPath variable).
Finally, you'll want to handle the row click in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, using the pattern XCode provides for you:
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Hope this helps!
That's a one-group UITableView with two rows (cells). The table style is UITableViewStyleGrouped. I'd recommend responding to selection of the cell using the delegate's –tableView:didSelectRowAtIndexPath: method as opposed to using UIButtons within the cell -- much nicer visual effect.
You can't group uibuttons like that. You can achieve this look-and-feel several ways, the most elegant is to use the grouped style uitableview.
However if you would want your buttons only to look like this, but not exactly like this, then you could also specify images for you buttons. Uglier way to do it, however much simpler ...