UITableView doesn't go into editing mode and it doesn't edit! - iphone

I'm hardly working on a new project. The main structure is a tableView in the RootViewController, where some rows shows the actual statuses and some informations to the user. I'm now working on the editing mode which won't work.
In the navigationbar is a button which calls edit here's the code:
- (void)edit {
NSLog(#"Edit pressed");
if (edit) {
NSLog(#"Deactivate edit\n");
edit = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Bearbeiten" style:UIBarButtonItemStyleBordered target:self action:#selector(edit)];
[contentTableView setEditing:NO animated:YES];
} else {
NSLog(#"Activate edit\n");
edit = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Fertig" style:UIBarButtonItemStyleDone target:self action:#selector(edit)];
[contentTableView setEditing:YES animated:YES];
}
}
My viewController is a subclass of UITableViewController and the contentTableView is not nil... I implemented the following methods:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
NSDictionary *movingDict = [[oldDict objectForKey:#"content"] objectAtIndex:sourceIndexPath.row];
[[oldDict objectForKey:#"content"] removeObjectAtIndex:sourceIndexPath.row];
[[oldDict objectForKey:#"content"] insertObject:movingDict atIndex:destinationIndexPath.row];
[saver write:oldDict];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *oldDict = [[NSMutableDictionary alloc] initWithDictionary:[saver read]];
[[oldDict objectForKey:#"content"] removeObjectAtIndex:indexPath.row];
[saver write:oldDict];
[contentTableView beginUpdates];
[contentTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //This doesn't work!
[contentTableView endUpdates];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"Entfernen";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
I copied this from another Project and changed some parts of code but the main things I didn't touch!
What happens?
When I press Edit ('Bearbeiten') the function "edit" gets called. It replaces the button with Done ('Fertig') and writes the correct log:
> 2011-04-27 13:35:44.338 MyApp[1113:207] Edit pressed
> 2011-04-27 13:35:44.339 MyApp[1113:207] Activate edit
If I slide on a cell, the button Remove ('Entfernen') appears and when I press it, the button disappears but the row is still there, but it's removed from the content.
Does anyone know where I'm doing something wrong or where I forgot something?
Thanks in advance, mavrick3.
Edit 1:
I forgot to say: The method [tableView reloadData]; doesn't call numberOfRowsInSection...

Those problems seems to be a bug in the iOS Simulator running iOS 4.3.2.
If you figured out this problem, please set your Deployment Target to an older SDK e.g. 4.2 and run your application on it.

Related

In ios7 tableView:didSelectRowAtIndexPath: is not called

I meagrate my project to adjust ios7 while I encountered a strange problem : the delegate method "tableView:didSelectRowAtIndexPath:" is not called in ios7 ,it works well in prior ios version.I was wonder if some specific property be changed in ios7
Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
[self.tableView setAllowsMultipleSelection:NO];
[self.tableView setMultipleTouchEnabled:NO];
}
- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[sender deselectRowAtIndexPath:indexPath animated:YES];
_currentContact = [contactArr objectAtIndex:indexPath.row];
if ([_currentContact.accountNPC hasPrefix:#"0"]) {
isContactToNPC = NO;
}else{
isContactToNPC = YES;
}
....
}
Add the following in your viewController.h file
<UITableViewDelegate,UITableViewDatasource>
also connect the tableview's delegate and datasource to the File's Owner of .xib
I'm sorry,I found the answer
I just need set
[cell setExclusiveTouch:YES]
Tks #abhishekkharwar from https://stackoverflow.com/a/18826264/2396477

uitableView didSelectRowAtIndexPath "selected row not loading the detail view"

I have a table view with 7 rows, I want to click on my rows and load the detailView( uiViewController )
I can select the row and I can see the Log in console but it never loads the detail view
would you please give me some hits, what is the problem?
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"selected rows");
MyBookDetailView *c = [[MyBookDetailView alloc] init];
[self.navigationController popToRootViewControllerAnimated:NO];
[self.navigationController pushViewController:c animated:YES];
}
I also try performSelectorOnMainThread but still it just clickable and I have problem to load my view controller, I also add delegate in - (void)viewDidLoad method,
Thanks in advance!
lates code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyBookDetailView *c = [[MyBookDetailView alloc] initWithNibName:#"MyBookDetailView" bundle:nil];
[self.navigationController pushViewController:c animated:YES];
}
Why you want to pop up all controllers withing navigationcontroller stack to root controller and same time your want to push up new MyBookDetailView (i hope its base class is UIViewController).
Anyways, MyBookDetailView *c = [[MyBookDetailView alloc] init]; also will not work for you. Because c object of UIViewController (MyBookDetailView) view is nil. I recommend use break point trace execution stack and variable that you trying to remove and adding on run time you will better know what going on in your program.
I think following code may work for you,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyBookDetailView *c = [[MyBookDetailView alloc] initWithNibName:#"WriteYourNibNameIfYouCreate" bundle:nil];
[self.navigationController pushViewController:c animated:YES];
}

show alert while deleting a uitableViewCell ios5

I am implementing a swipe to delete functionality in uitableview. I am able to achieve that, but now i wish to display a alert message to confirm delete. I have created the tableview programmatically.
ViewController.h
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tView;
NSMutableArray *myArray;
NSIndexPath *lastIndexPath;
}
#property(nonatomic,retain) UITableView *tView;
#property(nonatomic,retain) NSMutableArray *myArray;
#property(nonatomic,retain) NSIndexPath *lastIndexPath;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor lightGrayColor];
myArray = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10", nil];
// Do any additional setup after loading the view, typically from a nib.
tView = [[UITableView alloc] initWithFrame:CGRectMake(30, 30, 700, 800) style:UITableViewStylePlain];
tView.delegate=self;
tView.dataSource=self;
[self.view addSubview:tView];
[tView release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myArray count];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm Delete" message:#"Are you sure?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Delete", nil];
[alert show];
[alert release];
if (editingStyle == UITableViewCellEditingStyleDelete) {
lastIndexPath = indexPath;
NSLog(#"%#......%d",lastIndexPath,lastIndexPath.row);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
NSLog(#"cancel");
}
else {
NSLog(#"delete");
[myArray removeObjectAtIndex:lastIndexPath.row]; //memory error
[tView deleteRowsAtIndexPaths:[NSArray arrayWithObject:lastIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//..do required thing and return cell
}
I get a memory error in the clickButtonAtIndex method for alert view. I think it is for the lastIndexPath, but the nslog() give the correct value in lastIndexPath.
What am i doing wrong?
I hate it when answers just say "You should't do that"... So I'll explain why you are getting an error and then hopefully appeal to your better judgement as to why you really ought not to do this (from a UI point of view).
You are getting an error because you assign a value you do not own (you have not retained it) to an ivar directly. You probably meant to say self.lastIndexPath.
What happens is that the alert view does not really appear until the next time through the run loop at which point indexPath has been autoreleased and you try to access it through a pointer.
Changing lastIndexPath = indexPath; to self.lastIndexPath = indexPath; should solve the memory issue. (Since you marked the property as retain, assuming you synthesized it and did not write your own handler, when you use the self. prefix the synthesized accessor will retain it for you).
(Incidentally this is why is not a bad idea to name your ivars differently from your properties (e.g. Apple uses <property_name>_, on my site I use m<Property_name> it makes it hard to make this sort of mistake).
OK... But back to why you should not do this....
You user has, at this point, made a recognizable gesture by swiping left and has then pressed a red button marked 'delete'... And now you are going pop an alert and ask them to confirm? Really? OK, if you must...

how to delete a row in a section and hiding the section?

I am having an issue where when i have two sections let say section x and section. Section x has one row and also section y. If i delete a row in section y which is the second one, it will delete the row in section x and keeping the row in section y also it leave a space when i hide the section if their is a way to hide the section which wont make a mess please tell me a way here are some picture that might let u understand my question
and here is the code i use to delete a row
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
int path;
path = indexPath.row;
[data removeObjectAtIndex:path];
if([data count] == 0)
{
UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
self.navigationItem.rightBarButtonItem = leftbutton;
[leftbutton release];
[self.navigationItem.rightBarButtonItem setEnabled:FALSE];
[mainTableView setHidden:TRUE];
}
else
{
[self.navigationItem.rightBarButtonItem setEnabled:TRUE];
[mainTableView setHidden:FALSE];
}
[self saveData];
[mainTableView reloadData];
}
To remove a section when you have deleted all rows belonging to it, call - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation & return one less from - (NSInteger)numberOfSections.
You haven't added any code that shows how you are deleting a row from the table (removing it from data isn't sufficient). Try something like-
[self.tableView beginUpdates];
[data removeObjectAtIndex:path];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Well at the beginning i wasn't able to solve the issue but with the help of #Akshay my first problem was while deleting it always delete the section on top the section i was deleting to solve the issue i just had to update the number of rows in a section using
[mainTableView reloadData]; before the return expression

addSubview insertSubview aboveSubview bit confused as to why it does not work

I'm a bit confused all as to why the belowSubview does not work.
I'm adding some (subviews) to my navigationController and all of that works well.
-(UITableView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
...
[self.navigationController.view addSubview:ImageView];
[self.navigationController.view addSubview:toolbar];
add some point in my app I wish to add another toolbar or image above my toolbar.
so let's say I'm doing something like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
...
[self.navigationController.view insertSubview:NewImageView aboveSubview:toolbar];
//crucial of course [edit]
rvController = [RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle] mainBundle];
rvController.CurrentLevel += 1;
rvController.CurrentTitle = [dictionary objectForKey:#"Title"];
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController release];
However this doesn't work..
Does anyone know what I'm doing wrong here ...
Should I have used something else instead of addSubview or is the problem somewhere else?
From what you have posted, it looks like that should work.
There are a few other issues however. First it is convention that varibales that represent instances of objects start with lowercase. So ImageView and NewImageView should be imageView and newImageView.
I would make sure that in your tableView:didSelectRowAtIndexPath: method newImageView and toolbar are both valid. Are they in your header file?
Try this and see where is errors out:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
...
NSAssert(self.navigationController.view,#"WTF? self.navigationController.view is nil");
NSAssert([self.navigationController.view superview],#"WTF? lf.navigationController.view is not onscreen");
NSAssert(newImageView,#"WTF? newImageView is nil");
NSAssert(toolbar,#"WTF? toolbar is nil");
NSAssert([toolbar superview],#"WTF? toolbar is on in the view");
[self.navigationController.view insertSubview:newImageView aboveSubview:toolbar];
}