UITableViewCell selected on edit mode - ios

A UITableViewController is getting loaded with multiselection and ON EDIT mode.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
....
[self.tableView setEditing:YES animated:YES];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
The result is this one:
However this is not what I am looking for, since I want after the viewWillAppear some cells to be already selected.
I would like to be like this
What code do I need on - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ???
Do I need code in any other method?

You have to keep track of selected ones, that could be BOOL variable in your model, NSArray of indexPaths, or anything in between.
So what you should do in you cellForRowAtIndexPath:
if( dataModel.shouldBeSelected == true){
cell.imageView.image = [UIImage imageNamed:#"selected"];
}
else{
cell.imageView.image = [UIImage imageNamed:#"empty"];
}
note that you have to take care of the non-selected ones so you can prevent reuse of selected cells and showing incorrect results.

Related

How to create a animation for my tableviewcell images alone?

I have created expanded tableview using plist, I have populated all the values and images into my tableview. Here I want to make animation for my particular cell when user click that cell.
For example, first time images will be default, next time if I click that cell, the particular cell get expanded. Here I need to make my image has hide; again if user press it will show the image. How to do this?
Every time the user taps a cell, a UITableViewDelegate method gets called, and in this method you can reload the cell:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
When a cell is reloaded, the method below is called (between the others), and the only thing you have to do is to return a different height according with the current state of the cell:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (/* cell state */) ? 100.0 : 50.0;
}
You have multiple ways to store your cell's state, you can do it in its model for example.
There are other ways also, this is only a solution. But the key point is that you have to reload the cell, and return a different height according with the conditions you want to consider.
You can check my full solution in the following github
Here is some of my implementation.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
movies = [[NSArray alloc] initWithObjects:
(NSArray*)[[Section alloc ] init:#"Istanbul" movieNames:#[#"Uskudar", #"Sariyer"] isExpanded:false],
(NSArray*)[[Section alloc ] init:#"Bursa" movieNames:#[#"Osmangazi", #"Mudanya", #"Nilufer"]
isExpanded:false],
(NSArray*)[[Section alloc ] init:#"Antalya" movieNames:#[#"Alanya", #"Kas"] isExpanded:false], nil
];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return movies.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ((Section*)movies[section]).movies.count ;
}
-(void)toggleSection:(ExpandableHeaderFoorterView *)headerView withSection:(int)section
{
((Section*)movies[section]).expanded = !((Section*)movies[section]).expanded;
[expandableTableView beginUpdates];
for (int i= 0; i< ((Section*)movies[section]).movies.count; i++)
{
NSArray* rowsToReload = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:section], nil];
[expandableTableView reloadRowsAtIndexPaths:rowsToReload
withRowAnimation:UITableViewRowAnimationFade];
}
[expandableTableView endUpdates];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
ExpandableHeaderFoorterView* headerView = [[ExpandableHeaderFoorterView alloc] initWithCustom:((Section*)movies[section]).genre withSection:(int)section withDelegate:self];
return (ExpandableHeaderFoorterView*)headerView;
}
http://www.iostute.com/2015/04/expandable-and-collapsable-tableview.html
You can see this tutorial. It's a very good tutorial based on expandable tableview cell.

Hiding checkmarks in UITableView multiple selection during Edit Mode

I've got a UITableView which is automatically set up for multiple selection in Edit Mode using the following lines in viewDidLoad:
self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self setEditing:YES animated:YES];
However, I'd like to indicate that a row was selected by changing its background color, rather than by the checkmarks which automatically appear along the left of each row. (For example, the ones that appear when editing the email list in the Mail app, or being discussed in this SO question.) I've got it working for the most part, except that I can't get those checkboxes, which are automatically created as part of putting the UITableView into Edit Mode, to go away.
Below is the code I'm working with:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _Hierachy.cellCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *testCell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(testCell == nil) {
testCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
[[testCell textLabel] setText:#"Test Cell"];
return testCell;
}
Those are the only UITableView methods I've got so far, so everything else should be default behavior.
Does anyone know how to hide those checkmarks along the left, in Edit Mode? I've seen many questions about checkmarks in the accessory portion of the cell, but as I understand it, this is a different thing. I've also seen people talk about the tableView:didSelectRowAtIndexPath: method, but these checkmarks are created when the table enters Edit mode and dismissed when the user taps "Done," so that method doesn't seem related.
The closest I've come is finding this method:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
But that just prevents the cell's content from indenting to make room for the checkmarks. The checkmarks still appear.
Surely there's a way to hide those checkmarks, and still allow multiple selection in Edit Mode? Or are those checkmarks seriously mandatory behavior for a UITableView in Edit Mode with Multiple Selection enabled?
EDIT: I am (reluctantly) open to answers that are somewhat hack-y, like moving the frame of the checkmarks until it's off the screen. This app is for internal use, and won't need to be approved for the App Store. But given that the checkmarks are created automatically when the UITableView moves into Edit Mode, I don't even know how to get them as objects to alter. Any help would be appreciated!
You'll have to subclass your UITableViewCell and override the (void)setEditing:animated: method like this:
#import "MyCustomCell.h"
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView
{
//Cell Selected Color: CLEAR
[super setSelectedBackgroundView:selectedBackgroundView];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//Cell Edit Mode NO Indent & Selected Color: CLEAR
[super setEditing:NO animated:animated];
[self setNeedsLayout];
}
#end
After you do that, go to Inteface Builder and make your cell part of the class MyCustomCell.
After you make your cell part of MyCustomCell class in IB, import MyCustomCell.h in your UITableViewController and modify the following in your code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *testCell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(testCell == nil) {
testCell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
[[testCell textLabel] setText:#"Test Cell"];
return testCell;
}
UPDATE:
You could also do the following in your TableView's tableView:editingStyleForRowAtIndexPath:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
But you will get your cell indented. To remove that indent you'll have to subclass the Cell.
You should be good to go after doing this! I've just tested it and it works the way you want it!
Here is the most simple solution for multiple selection with no checkmarks:
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return NO;
}
This will cause the cells to be selected using the default selection style (gray or using your custom selection background), and no checkmarks will appear.
A word on whatever solution you pick. Users expect a consistent experience across multiple applications, and these checkmarks are part of this consistency. Make sure to have a good reason to change a normal OS look and feel.
here is how to achieve:
swipe to delete works
you don't see the checkbox, delete item or anything else on the left of the cell when in editing mode
Cell indentation still works normally (you can turn this off if you want)
(bonus) Support multiple selection
so - in your UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isEditing)
{
return UITableViewCellEditingStyleNone;
}
else
{
return UITableViewCellEditingStyleDelete;
}
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
(do stuff)
}
you also need to configure your UITableView
self.table.allowsMultipleSelectionDuringEditing=NO;
now if you actually do want multiple selection;
self.table.allowsSelectionDuringEditing=YES;
then manage the selected cells yourself.
I put a custom checkbox in my UITableViewCell subclass, and I also change the value in response to
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
The questioner wants to indicate selection with the background colour - that part should be straightforward.

Set Text Properties of UITableViewCell object dynamically with an array - cellForRowAtIndexPath never called

I am a beginner in Objective-C & iPhone development.
I add dynamically cells in a TableView. I want to set labels's text properties with an array. I saw many tutorials, and I searched during several hours but labels are never filled.
My code is :
- (void)insertNewObject
{
for (NSInteger ic=0; ic<((pages.count)); ++ic) {
NSLog(#"%d", ic);
NSDictionary *monDico = pages[ic];
menu = [monDico objectForKey:#"Name"];
NSIndexPath *indexPathTable = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPathTable] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; // I try include & exclude : never call
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = menu[indexPath.row];
NSLog(#"%#%#", #"Cell Label = ", cell.textLabel.text);
return cell;
}
Please note that insertNewObject method is called during viewDidLoad execution.
I use a breakpoint in the cellForRowAtIndexPath method : it never calls ! I try with :
explicit calling
forcing reloadData method
but did not work too.
Can you please tell me why ?
Thanks in advance.
If cellForRowAtIndexPath is not being called then most likely you have not set:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menu count]; // indicates the number of rows in your table view
}
This method needs to return the number of rows you expect to render within your table view. The default is 0 = no rows. I'm assuming you want to show all the items in your menu array so simply return [menu count].
Check this: UITableViewDataSource Protocol Reference
If you want to access to textLabel property of your cell, then it must be style of: UITableViewCellStyleDefault. Or, if you use storyboard, then set Cell's style to Basic.
And, of course, make sure that you have set delegate and datasource properties of your tableView.
- (void)viewDidLoad
{
[super viewDidLoad];
//...
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
------------------EDIT------------------
If you're using UITableViewController, then no more need to set delegate and dataSource properties manually, because they will automatically set by UITableViewController when your view did load.
If cellForRowAtIndexPath: method still not being called, then make sure that following methods that you implemented, both returns value >0:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Set a breakpoint before return and see returning values, or just NSLog them before returning.

How to limit the multi selection in UITableViewController

Here are tableView data source and delegate implementation, and I set the maximum selection is 5.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([multiOptions count] > 5) {
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
//show alert
}
...
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[multiOptions removeObject:selectedOption];
...
}
but here comes a question, if the options exceed the limit, the first click on cell will work just fine. but second time click the cell will call
didDeselectRowAtIndexPath
again, that's not what i expected, and I tried
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in didSelectRowAtIndexPath, it didn't work, can someone give me a hint, how to correct it? thanks.
You have to check this in tableView:willSelectRowAtIndexPath:
Have a look at the UITableView class reference
especially at the last sentence:
Return Value An index-path object that confirms or alters the selected
row. Return an NSIndexPath object other than indexPath if you want
another cell to be selected. Return nil if you don't want the row
selected.
So something like this will work within tableView:willSelectRowAtIndexPath::
if ([multiOptions count] > 5) return nil;

When UITableView is fully reloaded

I have a control that partially or fully changes content of tableView. After the change occurred, I set a flag tableViewContentHasChanged:
BOOL tableViewContentHasChanged = YES;
[self.tableView reloadData];
tableViewContentHasChanged = NO;
My problem appears in tableView:viewForHeaderInSection:; it is called after the table view is reloaded, so my flag is not effective inside that method.
In short: what's the right way to observe when the table has fully reloaded, so I could set the flag to NO? And, what am i possibly doing wrong?
I think the best way to handle this is in the data model as others mentioned but if you really need to do this, you can do the following:
According to Apple's documentation, only visible sections/cells are reloaded when you call reloadData
so you need to know when the last visible header is rendered so you set:
tableViewContentHasChanged = YES;
[self.tableView reloadData];
Then in cellForRowAtIndexPath: get the last displayed index and store it in a member variable:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Your cell creating code here
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"TryCell"];
//Set last displayed index here
lastLoadedSectionIndex = indexPath.section;
NSLog(#"Loaded cell at %#",indexPath);
return cell;
}
That way when viewForHeaderInSection: is called you'll know which is the last header in that reload event:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//Create or customize your view
UIView *headerView = [UIView new];
//Toggle tableViewContentHasChanged when it's the last index
if (tableViewContentHasChanged && section == lastLoadedSectionIndex) {
tableViewContentHasChanged = NO;
NSLog(#"Reload Ended");
}
return headerView;
}
Please note that this method will only work if last visible section has at least 1 row.
Hope this helps.

Resources