Tuesday, 14 July 2015

Delete a row in table view IOS

Delete a Row from Table View in IOS



Learn Objective C from here:Click Here

Learn Swift from Here:Click Here

Learn Initializers in Objective C:Click Here


Learn Inheritance in Swift:Click here

Learn IOS Application Development From here:Click Here

Learn Attractive Table View in IOS:Click Here.

Learn Accessory Type in IOS:Click Here

As you have seen in my last tutorials of OtherButton Title manages in  Alert View,Not Read:Click Here.

Deleting a row from table view is very easy just use of one method. In General Table View is represented in Normal Mode.
There is one method behind commitEditingStyle for table view which put table view in Editing mode.

It placed Delete button on right Side automatically.
General Delegate of table view search for EditingStyle method. If it find then when you swipe your row of table view to left it shows you delete button.

Add following code:

-(void)TableView:(UITableView *)TableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
It looks like below:



You can also delete a row from table view by long Pressing On row of table view but you need to add Gestures. But you are not familiar about gestures currently.
I will describe a tutorials about gestures deletion row lator on.


Now delete a row from array with removeObject,removeObjectAtIndex any method.


-(void)TableView:(UITableView *)TableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[studentdata removeObjectAtIndex:indexPath.row];
}

Now row is removed we need to call again Data because it assigned before application is start or during load view in viewdidload method. So table view need to load again.

just write [tableview reloaddata];

below removeObjectAtIndexPath method.

Now the row is deleted from TableView.

It look like:



What happens when you build code again the row comes again. It is due to Loading of Method before the view is loaded the data is loaded into array again. 


Above method of deletion is used on that cases where default values are taken and asked user to remove it according to your need. 

Above method of delete row from table view temporary.


Ok i will give you the solution of it so that Next time you start or build application it will manages array from new data not previous.

Add this code in commitEditingStyle method.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    lastElements=[studentdata objectAtIndex:indexPath.row];
    k=[studentdata indexOfObject:lastElements];
    NSUserDefaults *alltime=[NSUserDefaults standardUserDefaults];
    [alltime setInteger:k forKey:@"lastvalue"];
    [alltime synchronize];
    [studentdata removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}
Now add Below code in loadStudentData:
-(void)loadStudentData
{
    
    NSUserDefaults *alltime=[NSUserDefaults standardUserDefaults];
    l=[alltime integerForKey:@"lastvalue"];
    studentdata =[NSMutableArray arrayWithObjects:@"def",@"hjk",@"jko",
                  nil];
 int i;
        for (i=0; i<[studentdata count]; i++) {
            if ([studentdata objectAtIndex:l]==[studentdata objectAtIndex:i]) {
                [studentdata removeObjectAtIndex:l];
            }
        }
}

Now add in Interface the variables we used:
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

{
    NSMutableArray *studentdata;
     NSString *lastElements;
    NSInteger k;
    NSInteger l;
}

I have use Session but you are not familiar with NSUserDefault Currently. I will Describe it lator.
I have used it save Index of value which delete last time before the next time application start.

Then you would say when i build third time the application why the First row comes again.
The solution to it is i have used a single variable to store previous value you can add array so the value next will not passed.


Source Code Available here:Download here.

I hope you like my tutorials.Any query can mail me or comment here.





My next tutorials will be more options when Swipe the cell.


Monday, 13 July 2015

Other Button Title in Alert View IOS

OtherButtonTitle in UIAlertView IOS


Learn Objective C from here:Click Here

Learn Swift from Here:Click Here

Learn Initializers in Objective C:Click Here


Learn Inheritance in Swift:Click here

Learn IOS Application Development From here:Click Here

Learn Attractive Table View in IOS:Click Here.

Learn Accessory Type in IOS:Click Here

As you have studied my last tutorial of Attractive Table View in IOS if you have not read that tutorial: Click Here

Due to High Demand of One Visitor on my blog Today i am Published a post on Delegate in AlertView.







Let we start with UIAlertView.You have seen in last tutorials very heavily i used UIAlertView.
It is the Popup View appear above the main container or view of user interface.In UIAlert View Method you have seen code like below:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];

you see in above code we have take delegate nil due to nil use of otherButtonTitles.In CancelButtonTitle i have write OK when user click on it,Popup View is automatically dismiss due to method written for cancelButtonTitle already.OtherButtonTitle is make for user defined button linked with code.Also if user want to run their code with other button he can bind it.

What is delegate?

Delegate is Object responsible for behaviour of UIAlertView.

There are two ways to addOtherButtonTitles.
One way to add Other Button Title is:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
    [alert show];




Why i take delegate as Self?

Self means call with same object from which alert view is called.
I already describe the tutorials on Self and Super can see from here:Click Here.

Second way to write OtherButtonTitle is:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert addButtonWithTitles:@"Store"];
    [alert show];

Now you have seen i have added Store button on AlertView.But what happen when you click on it there is no code to executed it dismiss the popup view. you can also bind your code with Store Button.

There is method in Protocol UIAlertViewDelegate.If you have not studied about protocol you can: Click Here.

Add <UIAlertViewDelegate> at top of Interface in ViewController.h file.

 didDismissWithButtonIndex method is used which describe what code to execute when dismisspopupview with button index.
There is array of Indexes of button on AlertView. You can add multiple button according to your choice.
ButtonIndex=0 is used for CancelButtonTitle that is already described.
Furthor User defined Indexes start from 1 to your choice of end button.

Lets us look up about code with dismiss method:

 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }


Above method Run Message on Console when user click on the Store button.You can add code according to your choice like store value in string or something else.

There is one Othermethod

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }




The difference between above two methods are that clickedButtonAtIndex run when user click on Custom Button and then automatically dismiss pop up view.But didDismissWithButtonIndex runs code when popup view Dismiss.

The work of above code run same in both written methods.

Who Studied my Above tutorial carefully think that what happens if there are two Alert View in same class or more then two.The work of didDismissWithButtonIndex method is just to run code while dismissing popup not to check which alert view is executed.

Its solution is also possible.you need to add tag number with alert view.

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
[alert setTag:100];  
[alert show];



Now Another AlertView is:


UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"StudentDetail" message:@"DEF is Student Father" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
[alert setTag:200];  
[alert show];



Now the code will be in didDismissWithButtonIndex is:



 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
if([alertview tag]==100)
{
     if (buttonIndex==1) {
         NSLog(@"Store Button run successfully");
     }
 }
if([alertview tag]==200)
{
if(buttonIndex==1)
{
NSLog(@"Second Store button run successfully");
}
}


There are some readymade design for alert view.These are UIAlertViewPlainTextInput,UIAlertViewSecureTextInput,UIAlertViewLoginAndPasswordInput.

UIAlertViewPlainTextInput looks like:



UIAlertViewSecureTextInput:




UIAlertViewLoginAndPasswordTextInput:


Now problem arise how to fetch data from textfield in alertView.Its solution is also possible.

write below code:

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"StudentName" message:@"ABC is Student" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Store", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;  
[alert show];



 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex==1) {
          UITextField *logintext=[alertView textFieldAtIndex:0];
        UITextField *passwordtext=[alertView textFieldAtIndex:1];
        NSString *storestringlogin=logintext.text;
        NSString *storestringpassword=passwordtext.text;
        NSLog(@"Store Button run successfully:\n Login:%@ \n
     Password:%@",storestringlogin,storestringpassword);
     }
 }


Final Output will be :




There is also array of TextFields on Alert View.You can invoke with Index number which textfield to store data from it.

Above Code save Data in String and Display in Console with login and Password.

You can make Custom Alert View i will go on lator on it requires more coding.

I think it is very simple code you do not need any source code.If you need source code can ask from me by mailing,comment here.

I hope you like my tutorials.Any query can mail me or comment here.

My next tutorial will be Delete row from Table View.