0

I have a very tricky situation. And all things I have did programmatically.

1) I have UITableView.
2) In every cell of this tableview I have one uibutton.
3) On click of UIButton I want to show a UIAlertView, Which have yes and not buttons.
4) On click of yes button I want to delete that cell whose UIButton had shown up this alertview.

Below is my code snippet:

- (void) removeFriends:(id)sender
{
    /* 
     I want to show alertview at here...

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"CONFIRM",nil) message:NSLocalizedString(@"REMOVE_FRIEND",nil)  delegate:self cancelButtonTitle:@"No"  otherButtonTitles:@"Yes", nil];

     alert.tag = 21;

     alert.accessibilityIdentifier = @"Remove";

     [alert show];

     */

    UIButton *btn = (UIButton*)sender;

    int indx = btn.tag;
    NSString *friend_id = [friendsId valueForKey:[NSString stringWithFormat:@"%d",indx]];

    // URL creation
    NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"path"];

    NSString *address = [NSString stringWithFormat:@"%@%@%@", path,@"users/",usrId];

    NSURL *URL = [NSURL URLWithString:address];

    // request creation
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                       timeoutInterval:60.0];
    [request setHTTPMethod:@"DELETE"];

    responseData = [[NSMutableData alloc] init];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

    UITableViewCell *buttonCell = (UITableViewCell *)[btn superview];
    NSIndexPath* pathOfTheCell = [self.table indexPathForCell:buttonCell];

    [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:pathOfTheCell, nil] withRowAnimation:UITableViewRowAnimationFade];
    [self viewDidLoad];
    [self.table reloadData];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if([alertView.accessibilityIdentifier isEqualToString:@"Remove"])
    {
        if (buttonIndex == 1)
        {
            // here want to make call to  server
        }
    }
}
4

4 回答 4

0

In this situation, you can assign index of cell inalertview tag and use this tag to perform your operation.

Thanks

Update:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView.accessibilityIdentifier isEqualToString:@"Remove"])
{
    if (buttonIndex == 1)
    {
        // here want to make call to  server
        // User alertView.tag as index to delete  
    }
}

}

于 2013-09-04T12:44:15.230 回答
0

Just put this:

NSString *friend_id = [friendsId valueForKey:[NSString stringWithFormat:@"%d",indx]];


// URL creation
NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"path"];

NSString *address = [NSString stringWithFormat:@"%@%@%@", path,@"users/",usrId];

NSURL *URL = [NSURL URLWithString:address];

NSLog(@"%@",address);

// request creation
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                   timeoutInterval:60.0];

[request setHTTPMethod:@"DELETE"];

responseData = [[NSMutableData alloc] init];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

UITableViewCell *buttonCell = (UITableViewCell *)[btn superview];
NSIndexPath* pathOfTheCell = [self.table indexPathForCell:buttonCell];

[self.table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:pathOfTheCell, nil] withRowAnimation:UITableViewRowAnimationFade];
[self viewDidLoad];
[self.table reloadData];

into this:

if([alertView.accessibilityIdentifier isEqualToString:@"Remove"]){
    if (buttonIndex == 1) {

       // here want to make call to  server

    }
}

and create a property in your .h file which will be holding the id of the button which had been pressed. Like this:

//in your .h file
@property (nonatomic, strong) NSString *yourId;

and in your button-action just set it like this:

self.yourId = [friendsId valueForKey:[NSString stringWithFormat:@"%d",indx]];

In the first code snippet I guess you will have to replace usrId with self.yourId, so it will be

NSString *address = [NSString stringWithFormat:@"%@%@%@", path,@"users/",self.yourId];

EDIT: Remove this:

UITableViewCell *buttonCell = (UITableViewCell *)[btn superview];

and change this:

NSIndexPath* pathOfTheCell = [self.table indexPathForCell:buttonCell];

to this:

NSIndexPath* pathOfTheCell = [NSIndexPath indexPathForRow:self.yourId]

self.yourID is the indexPath.row value which was saved in your button.tag property.

于 2013-09-04T12:53:03.563 回答
0

view.h

@interface MyReservationViewController : UIViewController
{
    int Delete_row;
}

view.m

- (IBAction)delete_btn:(id)sender
{
    UIButton *buttontag = (UIButton *)sender;
    //NSLog(@"%d",buttontag.tag);
    Delete_row = buttontag.tag;

    UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Delete Reservation"   message:@"Are you sure to want Delete Reservation ??" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel",nil];
    Alert.tag=1;
    [Alert show];
    [Alert release];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(alertView.tag==1)
    {
        if(buttonIndex == 0)
        {
            NSLog(@"%d",Delete_row);
            //delete cell

            [Arr removeObject:Delete_row];
            [tbl reloadData];

        }
    }
}
于 2013-09-04T13:01:29.973 回答
0
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView.accessibilityIdentifier isEqualToString:@"Remove"])
{
    if (buttonIndex == 1)
    {
        // add server call to delete the relevant data
        // and then refresh your array that you are using in cellForRowAtIndex
        //and reload table. thats it.
    }
}
于 2013-09-04T13:37:59.897 回答