如何从类方法调用tableView

时间:2013-07-23 04:10:14

标签: 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];
    }];
}

2 个答案:

答案 0 :(得分:2)

为了获得滑动效果,您需要实现表视图委托

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

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

举个例子:

- (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];
}

希望有所帮助。

答案 1 :(得分:0)

NSArrays是不可变的,这意味着您无法添加或删除元素,因此您的对象没有removeObjectAtIndex:方法。

  • 检查您的媒体资源,看看它是NSArray而不是NSMutableArray
  • 检查该指针中的对象实际上是NSMutableArray
  • 要为变量指定NSArray,请先使用NSMutableArray方法从中创建arrayWithArray: