ios - 如何从类方法调用tableView

标签 ios objective-c uitableview instance-variables

我在从 tableView 中删除单元格时遇到了很多麻烦。我在滑过时显示的滑动单元格中有 3 个按钮,调用滑动的方法在 JBSliderViewController 中。为了访问嵌入在 homeViewController 中的类方法,我在 JBSliderViewController -(void)callDeleteFound 中创建了一个实例方法来调用我的 homeViewController 中的 +(void)deleteFound。

我现在的问题是我无法访问我的 tableView 以调用 – deleteRowsAtIndexPaths:withRowAnimation: 以及 currentTable 返回 null

有什么建议吗?

在 JBSliderController 内部

- (void)callDeletefound {
    [homeViewController deleteFound];

}

在 homeViewController 内部

+ (void)deleteFound {
    NSUserDefaults *userSessionData = [NSUserDefaults standardUserDefaults];
    NSString *userID = [userSessionData stringForKey:@"userID"];
    NSString *authKey = [userSessionData stringForKey:@"authKey"];
    NSString *appVersion = [userSessionData stringForKey:@"appVersion"];
    NSString *versionedURL = [NSString stringWithFormat:@"/ios/%@/", appVersion];
    NSURL *url = [NSURL URLWithString:@"http://website.com"];
    NSString *postPath = [NSString stringWithFormat:@"%@deletefind.php", versionedURL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            userID, @"userid",
                            [MyClass friendID], @"friendid",
                            authKey, @"tempAuth",
                            nil];
    [httpClient postPath:postPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SVProgressHUD showSuccessWithStatus:@"Removed from notifications"];
        NSMutableArray *notificationArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"notificationsArray"];
        NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:notificationArray];
        NSLog(@"%@", [MyClass cellNum]);
        NSInteger *num = [[MyClass cellNum] integerValue];
        [arrayThatYouCanRemoveObjects removeObjectAtIndex:num];
        [currentTable deleteRowsAtIndexPaths:openedCellIndexPath withRowAnimation:UITableViewRowAnimationLeft];
        [currentTable reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Fail" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
}

最佳答案

为了获得滑动效果,您需要实现 TableView 委托(delegate)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

方法,这将提供对用于删除的滑动交互的访问。我通常也为可以删除的 TableView 提供编辑交互,因为滑动交互往往对用户有点隐藏。

举个例子:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView beginUpdates];    
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Do whatever data deletion you need to do...
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];      
  }       
  [tableView endUpdates];
}

希望对您有所帮助。

关于ios - 如何从类方法调用tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17801340/

相关文章:

ios - 复制 png 错误,退出代码为 5

ios - 当应用程序从重定向的应用程序重新获得焦点时的回调

ios - 空 NSArray 的后端服务超出范围

ios - 如何从 iPhone 视频录制中实时捕捉逐帧图像

ios - 自定义 UITableViewController 结构

ios - tableView beginUpdates/endUpdates block 重新加载整个表,即使为空

iphone - 如何编辑 NSMutableArray 数据?

ios - 我可以在 iOS 12 全屏模式下抑制警报 "It looks like you are typing while in full screen"

ios - 刷新 UITableView 而不刷新节标题

ios - 代码 : compositing with alpha using core image