Hide sectionindextitles when delete button appears on the cell - iphone

I am setting titles for Tableview using SectionIndexTitlesForTableview method. When I swipe a cell, delete button appears next this titles which looks odd. How to hide this indexTitles when delete button appears and show when delete button disappears.

The inEditMode method hides an index when a table is going to be edited
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self inEditMode:YES];
}
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self inEditMode:NO];
}
//on self.editButtonItem click
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self inEditMode:editing];
}
-(void)inEditMode:(BOOL)inEditMode{
if (inEditMode) { //hide index while in edit mode
self.tableView.sectionIndexMinimumDisplayRowCount = NSIntegerMax;
}else{
self.tableView.sectionIndexMinimumDisplayRowCount = NSIntegerMin;
}
[self.tableView reloadSectionIndexTitles];
}

This might help
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [tableView isEditing] ? nil: #[#"A",#"B",#"C"];
}
- (void)setEditing:(BOOL)editing
{
[super setEditing:editing];
[self reloadSectionIndexTitles];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
_someBoolean = YES;
[tableView reloadData];
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
_someBoolean = NO;
[tableView reloadData];
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _someBoolean ? nil : _yourTitlesArray;
}

Related

How to set selection backGround for the entire section in u itable view

I Have a table view with multiple sections as follows..
I know how to set background color for the selecting a Cell / row
But i need to fill the selection to the entire section instead of individual cell selections
is it possible if yes how any solution
i hope its working....
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.isSelected == YES)
{
[cell setBackgroundColor:SelectedCellBGColor];
}
else
{
[cell setBackgroundColor:setBackgroundColor:NotSelectedCellBGColor];
}
}
Pass the section value from the didSelectRowAtIndexPath delegate of tableview and change the background color of the cell in cellForRowAtIndexPath dataSource method of UITableview
in Header File
NSUInteger index;
in Implemention File (.m)
-(void) viewDidLoad{
index=-1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==index) {
cell.contentView.backgroundColor=[UIColor yellowColor];
}
else{
cell.contentView.backgroundColor=[UIColor clearColor];
}
cell.textLabel.text=[NSString stringWithFormat:#"Row %d Section %d",indexPath.row,indexPath.section];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
index=indexPath.section;
[tableView reloadData];
}
.h
int currentSection;
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
currentSection = indexPath.section;
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( currentSection == indexPath.section ) {
cell.backgroundColor = SelectedColor;
} else {
cell.backgroundColor = OtherColor;
}
}

iPhone: UITableView reorder rows button

I have UITableView with custom table row. What I need is to have possibility to reorder rows. so in ViewDidLoad i add:
- (void)viewDidLoad {
[super viewDidLoad];
[self.myTableView setEditing: YES animated: YES];
}
also add next methods:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Also I set bg color for my custom table row:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *customTableRow = #"customTableRow";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
customTableRow];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customTableRow"
owner:self options:nil];
if (nib.count > 0) {
cell = self.customTableRow;
}
}
cell.contentView.backgroundColor = [UIColor colorWithRed:(228.0f/255.0f)
green:237.0f/255.0f blue:244.0f/255.0f alpha:1.0];
return cell;
}
And then I run my app and I had an unexpected view:
So I have tow questions:
1.So why it so ? Reorder button is not transparent ??
2.How to change that button to my on image ??
The problem is that you're changing the background color of the contentView. Have you tried changing the background color of the cell itself in -tableView:willDisplayCell: forRowAtIndexPath:?

iPhone:Add & Delete Row in UITableview with editing mode

What I want to do is:
Setting editing mode on NavigationBar in UITableView adds an edit button on the left side of the UINavigationBar. When I click this button, I'd like an add button to appear on the right side of the NavigationBar.
When I click on the add button, add a row to the NSMutableArray and update the table.
So please give me ideas, code, or links to develop this functionality.
- (IBAction)DeleteButtonAction:(id)sender
{
[arry removeLastObject];
[Table reloadData];
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([arry count]))
{
return UITableViewCellEditingStyleInsert;
} else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arry removeObjectAtIndex:indexPath.row];
[Table reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
[arry insertObject:#"Tutorial" atIndex:[arry count]];
[Table reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[arry objectAtIndex:fromIndexPath.row] retain];
[arry removeObject:item];
[arry insertObject:item atIndex:toIndexPath.row];
[item release];
}
}
Create a private property to have a reference to the Edit button:
#property (nonatomic) UIBarButtonItem *editButton;
In viewDidLoad:, init the button and assign it to the right button of the navigationItem:
self.editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Edit", nil) style:UIBarButtonItemStylePlain target:self action:#selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = self.editButton;
Create a method to invoke when the Edit button is tapped. Will change the table's editing mode and update the Edit button title:
- (IBAction)toggleEdit:(id)sender {
[self.myTableView setEditing:!self.myTableView.editing animated: YES];
[self.editButton setTitle:self.myTableView.editing ? NSLocalizedString(#"Done", nil) : NSLocalizedString(#"Edit", nil)];
}

Delete a row in a grouped tableview

Well something is wrong but i dont know what. I cannot delete a row . when my tableview get in editmode red minus signs near all rows appear . touch on them make to appear delete button on row , whem i touch it it become (dark red) and nothing happens.
here is my code (not all) + i have no errors and warnings at compilation and runtime ...
- (void)viewDidLoad {
[super viewDidLoad];
// initialize singleton.
appDelegate = (ExchangedAppDelegate *)[[UIApplication sharedApplication]delegate];
// get banks from settings.plist
NSString *tempPath = [self getPathOfSettingsPlist];
appDelegate.banks = [[NSArray alloc]initWithContentsOfFile:tempPath];
navigationItem.rightBarButtonItem = self.editButtonItem;
backButton = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonSystemItemCancel target:self action:#selector(closeSettingsView)];
navigationItem.leftBarButtonItem = backButton;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[banksTableView setEditing:editing animated:animated];
if (editing) {
addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addBankFromList)];
navigationItem.leftBarButtonItem = addButton;
} else {
navigationItem.leftBarButtonItem = backButton;
NSString *tempPath = [self getPathOfSettingsPlist];
self.picker.hidden = YES;
[appDelegate.banks writeToFile:tempPath atomically:YES];
}
}
- (void)addBankFromList {
// not interesting
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.banks count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"BanksTableCell";
indexForPath = indexPath;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellEditingStyleNone;
}
cell.textLabel.text = [appDelegate.banks objectAtIndex:indexPath.row];
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle: forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[appDelegate.banks removeObjectAtIndex:indexPath.row];
[banksTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[banksTableView reloadData];
}
}
//// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSString *temp = [appDelegate.banks objectAtIndex:fromIndexPath.row];
[appDelegate.banks removeObjectAtIndex:fromIndexPath.row];
[appDelegate.banks insertObject:temp atIndex:toIndexPath.row];
}
Actually you have added an extra colon(":") in the commitEditingStyle: method name. Adding the extra colon has made this method a completely different method. So, your view controller thought that you haven't implemented the commitEditingStyle: method and didn't do anything while you edit your table view.
Just remove the colon(":") after editingStyle in the commitEditingStyle: method. That will fix the problem. That line should look like this,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

editingStyleForRowAtIndexPath isn't getting called (So a delete button is showing up)

I'm playing around with moving uitableviewcells, and for whatever reason,
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingstyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone; //<-- bp set on it
}
isn't getting called (I've set a breakpoint on it) - so the table is showing the delete option, but I don't want that. Here's my implementation:
#implementation MoveItController
#synthesize mList;
- (IBAction)moveButton
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
[self.navigationItem.rightBarButtonItem setTitle:(self.tableView.editing)? #"Done" : #"Move"];
}
- (void)viewDidLoad
{
if (mList == nil)
{
mList = [[NSMutableArray alloc] initWithObjects:#"$1", #"$2", #"$5", #"$10", #"$20", #"$50", #"$100", nil];
}
UIBarButtonItem *mvButton = [[UIBarButtonItem alloc]
initWithTitle:#"Move"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(moveButton)];
self.navigationItem.rightBarButtonItem = mvButton;
[mvButton release];
[super viewDidLoad];
}
// Table datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *moveItCellId = #"moveItCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:moveItCellId];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moveItCellId] autorelease];
cell.showsReorderControl = YES;
}
cell.textLabel.text = [mList objectAtIndex:[indexPath row]];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingstyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
id object = [[mList objectAtIndex:[fromIndexPath row]] retain];
[mList removeObjectAtIndex:[fromIndexPath row]];
[mList insertObject:object atIndex:[toIndexPath row]];
[object release];
}
- (void) dealloc
{
[mList release];
[super dealloc];
}
#end
No warnings at compile time.
Thanks!
You need to set multiple selection to NO in editing mode
self.tableview.allowsMultipleSelectionDuringEditing = NO;
Try capitalizing the method name properly
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
That's with the S in editingStyleForRowAtIndexPath capitalized. ObjC selectors are case sensitive. The table view does not think it's delegate responds to the method you are trying to provide a return value to.
Another reason might be that you also need to implement
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
Another reason for editingStyleForRowAtIndexPath (and other delegates) not being called is if the control does not have it's delegate outlet connected to the File's Owner.
Yes, this is obvious but it's easily overlooked.