In my iPad app I'm using Apple's standard split view template. The master view has a table that's populated by a plist file and my table is split into three grouped sections. In my viewDidLoad method I've added the editbuttonitem that works as expected - tap it and you can then delete rows.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
However, I'd like the editbuttonitem, when selected, to only allow editing in the last two sections and not the first. Is this possible? And if so, can someone help point me the way?
Or will I need to not use Apple's built-in mechanism for this and have to code a unique barbuttonitem that can perform this task?
Much appreciated.
Try implementing the editingStyleForRowAtIndexPath method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
return UITableViewCellEditingStyleNone;
else
return UITableViewCellEditingStyleDelete;
}
Related
I am working on an app where I need to reorder rows of section 1. I could achieve the reorder by implementing the tableView delegates. When the table is in editing mode I show reorder for section 1 and no controls for rest sections, but the rows of rest section should be deleted by swipe to left.
I am not sure whether this is possible but my requirement is exact the same.
Work done by me:
Below are the delegates of tableView I implemented:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.myTable isEditing]) {
return UITableViewCellAccessoryNone;
}
return UITableViewCellEditingStyleDelete;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return YES;
}
return NO;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
The above code made the edit mode look as I wanted. Re-order sign got visible for only section 1 & red delete button is also not visible for rest sections (as desired).
Problem:
The rows of sections apart from section 1 were also not being deleted.When I swipe to left nothing happens.
In short in edit mode, section 1 should be re-order enabled and rest sections should work as they work in normal mode i.e swipe left to delete row should be functioning in tableview edit mode.
AFAIK you cannot achieve what you are trying. The two sections are part of the same table which is being edited. So once the table is in editing mode, it will affect all the sections and rows.
what you can do is divide the data in the two sections into two separate tables and load the tables in the header view of each section like this
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
TableViewController1 *tab1 = [[TableViewController1 alloc] initWithStyle:UITableViewStylePlain];
return tab1.tableView;
} else{
TableViewController2 *tab2 = [[TableViewController2 alloc] initWithStyle:UITableViewStylePlain];
return tab2.tableView;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return self.parentTable.frame.size.height/2;
}
tab1 will have data from first section and similar case for tab2 (forgive the variable naming). The parent table should have grouped style and also scrolling should be disabled for it. This way the two sections can be edited independent of each other. Also, this categorises data and each section becomes independently scrollable. Hope this helps and answers what ur looking for.
Is there a way to disable all of the cells within a UITableView that aren't being used? Within my application a user chooses a max of two cells and after two cells are selected I want all other cells to be disabled but I can't figure it out. Any help would be greatly appreciated, thanks!
You don't want to disable the rows, you want to prevent the user from selecting additional rows, right?
Implement the tableView:willSelectRowAtIndexPath: delegate method. If there are already two rows selected you should return nil, otherwise return the index path.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (/* check if there are already two selected rows */) {
return nil;
} else {
return indexPath;
}
}
When opening a UITableViewController, I'd like to display a screen saying "nothing here, yet" (c.f. screenshot of the Dropbox App below) instead of empty rows when there are no sections/rows. This is done in multiple Apps already, but I'd like to know if there is a recommended way of achieving this.
I can imagine multiple ways of doing this, including
adding a UIView as a subView to the view of the tableViewController
switching to a UIViewController and implementing the tableView behavior on my own
setting the UIView as the backgroundView and hiding the tableView cells
Thank you for all your ideas!
The way I usually do it, is displaying a single custom cell when there's nothing to display.
There is a new (as of June 2014) library called DZNEmptyDataSet handling exactly this case. It uses the first approach (UIView as a subview) described in the question. Pretty neat realisation.
You must be having some data array through which you can define few things, in table view.. You can achieve this in your tableview only with its delegate methods..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([dataArr count] == 0)
return 1;
return [dataArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
return self.view.frame.size.height;
}
return 44.0;
}
and then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
//Create a cell with the no data image
tableView.scrollEnabled=NO;
}else{
//Create your default cell with data..
tableView.scrollEnabled=YES;
}
return nil;
}
I am just giving you an idea, its your choice of using it..
I have a tableview that loads with isEditing = YES. This is a bit of a hack (but a reasonable one I think) because I want one of my cells to always display the 'add' icon. I don't want the other cells to include the 'delete' icon so I am returning UITableViewCellEditingStyleNone for these.
However, I would still like to provide swipe to delete functionality on those cells. Since the tableview is already in edit mode, is this going to be possible?
If not, I will probably achieve the 'add' icon using the cells imageView, but this seems hacky too.
Edit: Similar question found here:
swipe to delete when already in edit mode
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[myArray removeObjectFromindex];
}
[mytableview reloadData];
}
This is what I am trying to do...but not sure how to do :
I have a tableview consisting of multiple rows in multiple sections. When I scroll the table going from 1 section to another, I want to do some other action - I want to have an action handler for this. But, I am not sure how to do it.
Cany anyone please help me out ? Thanks.
You can set the tableview delegate as the controller and implement this delegate function
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This function will get called before a cell is going to be displayed. You can use the indexpath to check if it is going to be first item in a section.
Hope this helps.