How to get the alert message? - iphone

I have this code and actually i want an alert message when user Tap the delete message ?? how can i do that ..
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *DeleteMeCellIdentifier = #"DeleteMeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DeleteMeCellIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

Please try this code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:[NSString
stringWithFormat:#"deleted row no. %#",indexPath.row] delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}

You can try this:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:[NSString stringWithFormat:#"You have deleted row no. %#",indexPath.row] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}

Related

Getting bad access while deleting cell and reloading table view

I am trying to build a table using custom UITableViewCell, but after deleting cell and when I try load the view, I get an EXC_BAD_ACCESS error,here i'm getting data from a dictionary and loading it to table view.
The code for my Table is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = (uploadCustomCell *)[tabelView1 dequeueReusableCellWithIdentifier:#"cell"];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"uploadCustomCell" owner:self options:nil];
cell = (uploadCustomCell *)self.uploadCustomcell;
}
saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(#"dict dict :%#",dictionary);
//
cell.nameLabelCell.text = [dictionary valueForKey:#"VideoName"];
cell.userName.text = [dictionary valueForKey:#"User"];
NSString *defaultVideo = [dictionary valueForKey:#"DefaultVideo"];
if ([defaultVideo isEqualToString:#"1"]) {
[cell.defaultSwitch setOn:YES animated:YES];
}
else {
[cell.defaultSwitch setOn:NO animated:YES];
}
[cell.defaultSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventValueChanged];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
VideoNameTextField.hidden = YES;
return cell;
}
- (void)setState:(id)sender {
state = [sender isOn];
// NSString *rez = state == YES ? #"YES" : #"NO";
NSLog(#"state.........:%d",state);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"height:%f",uploadCustomcell.frame.size.height);
return 207;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(#"Dictionary:%#",dictionary);
NSLog(#"indexpath:%#",indexPath);
//
NSLog(#"at index%d obj:%#",indexPath.row,dictionary);
NSString *nameDetails = [dictionary valueForKey:#"VideoName"];
guid = [dictionary valueForKey:#"GUID"];
detailsNameLbl.text = nameDetails;
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tabelView1 reloadData];
}
}
Try this:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
You must use this method also:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tabelView1 reloadData];
}

How to perform Validation in ios uitableview (group table)?

In my app i have used UItableview group table.
In which there are two section and i want to perform validation on it.
for eg:In section 1 there are two row's,if user select row one user have to select some value from section 2 and if user have select second row in 1 section then no need of selection in section two.
following is my code:
- (void)viewDidLoad
{
NSArray *arr_data1 =[[NSArray alloc]initWithObjects:#"Yes",#"No",nil];
NSArray *arr_data2 =[[NSArray alloc] initWithObjects:#"Monotherapy",#"Adjunctive",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arr_data1,#"",arr_data2,
#"Was it monotherapy or adjunctive",nil];
self.tableContents =temp;
self.arr_data =[[self.tableContents allKeys]
sortedArrayUsingSelector:#selector(compare:)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arr_data count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.arr_data objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSLog(#"fdfdfdf=%d",[indexPath row]);
NSString *rowValue = [listData objectAtIndex:row];
if ([rowValue isEqualToString:#"Yes"])
{
NSString *message = [[NSString alloc] initWithFormat:#"%#",rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
//do as per you requirement
}
else if(indexPath.section == 1)
{
//do as per you requirement
}
}
You can first declare a variable count to keep track of how many items are selected in section 2 and isItemRequired as a flag variable and sythesize them, then use these delegates:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
A sample code to begin with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
isItemRequired = YES;
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"You must have to select one or more item in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
else
{
isItemRequired = NO;
}
}
else if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"Atleast one item should be selected in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
}
In the didselect method of UITableView, you needs to check the section number & then check which row is clicked from that section
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
//do this
}
else if (indexPath.row == 1)
{
//do this
}
}
else
{
//do this
}
}
In the following delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
add a check with the help of indexPath returned by the delegate, it will tell you that which section's which row is selected like :
if(indexPath.section == 0 && indexPath.row == 0)
{
// Select something from the another section also
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
// No need to select from another section.
}
First Take one BOOL variable with isYes in .h file like bellow...
BOOL isYes;
after in viewDidLoad: method just assign it to NO like bellow..
isYes = NO;
after that in didSelectRowAtIndexPath delegate method just use like bellow...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0)
{
isYes = YES;
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
isYes = NO;
}
if(isYes){
if(indexPath.section == 1 )
{
//here user can select the section 2
}
}
}
i hope this help you....

how to implement code for saving multiple array items through the alertview?

I have implemented code for alert view with textfield here when i click on ok button the textfield entered value is storing in string formate as (str) and i am adding that value to the array in order to i am loading array to the table view but here problem is every time table view consists one item but i want store multiple items entering items from alert and save it as array item for this how to implement code for saving multiple array items through the alertview any one help me to solve this problem in iphone.
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year"
message:#"alert message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:#"" label:#"entervalue"];
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
str = alertTextField.text;
[savedarray addObject:str];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
str = alertTextField.text;
[savedarray addObject:str];
[self.yourtableview reloadData]; // reload your tableview when add new data in array.
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return savedarray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [savedarray objectAtIndex:indexPath.row];
return cell;
}
Hope, this will help you..
If i am rit u r asking for something like this:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"])
{
str = alertTextField.text;
if([savedarray Count] < yourRequiredItemsCount)
{
[savedarray addObject:str];
alertTextField = #"";
[alertView show];
}
else
{
[alertView release]; //Release the Alert here.
}
}
}
tell me if it helps! will give u an alternate sol.
If you're allocating the savedarray correctly, then the posted code looks reasonable. The next thing to check is whether you are properly using savedarray as the datasource for the table.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return savedarray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [savedarray objectAtIndex:indexPath.row];
return cell;
}
Be sure to set the same view controller as the tableview's delegate and datasource.

Custom delete button in uitableview cell don't work

I made tableView with filteredReservations array wich contains Rezervacija objects.
I added custom button "board", and when button is clicked, I ask with alert view to confirm this and then sending request to server. When receive server's response I need to delete row from table and object from filteredReservations array.
When I delete first row it's ok, but after that in filteredReservations instead of Rezervacija object, I got UIButton objects! I don't know HOW :)
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return([filteredReservations count]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
Reservation *reservation = [filteredReservations objectAtIndex:indexPath.row];
NSString *label = [[NSString alloc] initWithFormat:#"%# (%d)", reservation.name, reservation.seatsNumber];
cell.textLabel.text = label;
[label release];
[reservation release];
tableView.allowsSelection = FALSE;
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cellButton setFrame:CGRectMake(430.0, 2.0, 106.0, 40.0)];
[cellButton setTitle:#"Board" forState:UIControlStateNormal];
[cellButton addTarget:self action:#selector(BoardPassengers:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cellButton];
cellButton.tag = indexPath.row;
return cell;
}
-(void)BoardPassengers:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Question" message:#"Do you want to board passengers?"
delegate:self cancelButtonTitle:#"Odustani" otherButtonTitles:#"Da", nil];
[alert show];
[alert release];
passengerForBoarding = [NSIndexPath indexPathForRow:((UIControl*)sender).tag inSection:1];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(#"cancel");
}
else
{
Reservation *reservationNew = [filteredReservations objectAtIndex:passengerForBoarding.row];
NSString *reservationId = [[NSString alloc] initWithFormat:#"%d",reservationNew.reservationId];
params = [NSDictionary dictionaryWithObjectsAndKeys: #"1", #"tour_id", #"1", #"bus_number",reservationId, #"reservation_id", nil];
[[RKClient sharedClient] post:#"/service/boardingToBus.json" params:params delegate:self];
[DSBezelActivityView newActivityViewForView:self.view withLabel:#"Boarding..."];
}
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if([[request resourcePath] isEqualToString:#"/service/boardingToBus.json"]){
if([[response bodyAsString] isEqualToString:#"ERROR"]){
[DSBezelActivityView removeViewAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Error."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}else{
[DSBezelActivityView removeViewAnimated:YES];
[filteredReservations removeObjectAtIndex:passengerForBoarding.row];
[self.tableView reloadData];
//[self.tableView beginUpdates];
//[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:passengerForBoarding]
// withRowAnimation:UITableViewRowAnimationFade];
// NSLog(#"%i", passengerForBoarding.row);
//[self.tableView endUpdates];
}
}
}
It sounds like you need to make sure the table view is explicitly aware of how many filtered reservations it needs to display.
// assuming only one section for your table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return([filteredReservations count]);
}
For more info:
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection:
Hey you can get the value of "Reservation ID" By this.
-(void)BoardPassengers:(id)sender{
Reservation *reservationNew = [filteredReservations objectAtIndex:[sender tag]];
NSString *reservationId = [NSString StringWithFormat:#"%d",reservationNew.reservationId];}
Or you can use this value anywhere you want like you have mentioned via alert-box.

How to get all rows of same group in TableView

Please help me to get all rows of same group in tableView. i am using below code to show radio/check in groups of tableview and want the only one per group selection.
so for that i have got 2 images
1:uncheck
2:checked
so when user will click/select a row of group than i am changing its cell.image to "checked" and its working but don't know how to make all other cell's images of same group's to "unchecked"
dont know how to loop through (get) all rows/cells of current group
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:#"checkbox-checked.png"];
NSString *message = [[NSString alloc] initWithFormat:rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Here is a possible workaround:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Store the section in your class
NSUInteger section = indexPath.section;
self.section = section; // remember to declare instance variable, property, synthesize
self.row = row; // remember to declare instance variable, property, synthesize
[tableView reloadData];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == self.section) {
if (indexPath.row != self.row) {
// then do UNCHECK here
}
}
}
If you know how many rows in your current section, you don't need to call reloadData, just call [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION];