How to disable all unselected cells in a UITableView - ios

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;
}
}

Related

How show plain picture or plain text when no data available in TableView in objective c?

I have a table view used for showing my favourite video list. if no data available in the table view it will show this.
If you want to show it inside the tableView specifically then you will have to create a cell for the tableView with same structure and handle the cell used made based on wether or not data is present. If no data is present, you will have to instantiate a different cell and when data is present, a different one.
Here is how i would do it instead:
You can initially set the tableView to hidden. And create a view with the required imageViews and labels and also set it to be hidden. Make this view outside and not as the cell. On fetching data, depending on the response, you can unhide one of the views.
Try following code.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (arr.count==0) {
return 1;
}
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *emptyMessageCellIdenifier = #"emptyMessageCell";
NSString *datacellIdentifier = #"datacell";
UITableViewCell *emptyMessageCell ;
UITableViewCell *datacell ;
if (arr.count == 0) {
return emptycell;
}
else{
return datacell;
}
}
I got the solution.It's so easy. I take a uiview. and when list is empty, hide table view and show that uiview. It's really easy.

Show re-order sign for one section while rest sections should not show any controls of a tableView, and enable swipe left delete in edit mode

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.

iOS5 - Limit allowsMultipleSelection

I need to limit the number of multiple selection in a Table View, but I don't have any idea for where can I begin.
Thanks and regards!
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.indexPathsForSelectedRows.count > 5)
return nil;
else
return indexPath;
}
Create a NSMutableArray. Whenever someone selects a cell, check the number of items already in the array. If it's less than your limit, put a reference to that cell's backing data into your array. If it has reached its limit, either ignore the selection or replace a previous selection...whichever makes sense for your app.

Limit rows that editbuttonitem affects

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;
}

UITableViewCell: Allowing Selective Deletion

I have a table view and want to allow reordering of all cells, however there are certain cells that I do not want to be allowed to be deleted. when the UiTableView is put into deletion mode I do not want the red '-' button to appear on the left hand side, and do not want the swipe gesture to bring up the Delete button of these cells but want it to happen for the others. Any ideas?
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//if we cant delete the object represented at the index path
if ([[tableViewObjectsArray objectAtIndex:indexPath.row] canBeDeleted] == NO){
return UITableViewCellEditingStyleNone;
}
//otherwise allow the deletion
else{
return UITableViewCellEditingStyleDelete;
}
}
Of course this leaves an empty space where the '-' button should be, but it does not allow deletion. And also does not allow the swipe deletion either.
implement:
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}

Resources