我有一个非常棘手的情况。我所做的一切都是以编程方式完成的。
1)我有UITableView
2)在这个tableview的每个单元格中,我都有一个uibutton
3)点击UIButton
我想显示一个UIAlertView
,其中有“是”而非“按钮”
4)单击是按钮我想删除UIButton
已显示此警报视图的单元格。
以下是我的代码段:
- (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
}
}
}
答案 0 :(得分:0)
在这种情况下,您可以在alertview
标记中指定单元格索引,并使用此标记执行操作。
由于
更新
-(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
}
}
}
答案 1 :(得分:0)
就这样说:
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];
进入这个:
if([alertView.accessibilityIdentifier isEqualToString:@"Remove"]){
if (buttonIndex == 1) {
// here want to make call to server
}
}
并在.h文件中创建一个属性,该属性将保存已按下的按钮的ID。像这样:
//in your .h file
@property (nonatomic, strong) NSString *yourId;
并在你的按钮动作中设置如下:
self.yourId = [friendsId valueForKey:[NSString stringWithFormat:@"%d",indx]];
在第一个代码段中,我猜您必须将usrId
替换为self.yourId
,因此它将是
NSString *address = [NSString stringWithFormat:@"%@%@%@", path,@"users/",self.yourId];
编辑: 删除它:
UITableViewCell *buttonCell = (UITableViewCell *)[btn superview];
并改变这一点:
NSIndexPath* pathOfTheCell = [self.table indexPathForCell:buttonCell];
到此:
NSIndexPath* pathOfTheCell = [NSIndexPath indexPathForRow:self.yourId]
self.yourID
是保存在indexPath.row
媒体资源中的button.tag
值。
答案 2 :(得分: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];
}
}
}
答案 3 :(得分: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.
}
}