我是iOS新手,我遇到了問題。我有兩個UIViewControllers
第一個是ResponsesViewController(UITableView
),第二個DetailResponsesViewController。當我選擇一行ResponsesViewController,DetailResponsesViewController被推 。作爲我的後端,我正在使用parse.com。當解散ViewController時從tableView中刪除行和對象
ResponsesViewController填充有Parse.com對象,當我選擇一個行,DetailResponsesViewController被推與從選擇的行的對象。我解決了這個問題,但是我有一個backButton在DetailResponsesViewController,沒有其他方法如何解除這個控制器,只有當用戶點擊backButton而沒有其他方法時如何刪除對象,只能在DetailResponsesViewController點擊這個backButton。當點擊按鈕時,我想要從parse.com中刪除對象,關閉控制器並從ResponsesViewController(刪除具有該對象的行)刪除對象,在下面的代碼中,我有一些解決方案,但它沒有' t正常工作,它每次都從服務器上刪除對象,這是可行的,但它不會從UITableView
非常時間刪除它,例如,我有10行,並且每第3行都不會被刪除,並且當我彈出時tableView from DetailResponsesViewController它仍然可以在tableView中看到,但從服務器中刪除,當我從菜單中選擇另一個VC並再次返回時,它會消失。
我嘗試了很多解決方案,把[self.tableView reloadData]
,大多數情況下到處都是,但仍然只刪除有時從TableView行。
它的工作原理與SnapChat相同,但沒有定時器:)當用戶從tableView中讀取消息時,它將被刪除。
有誰知道,問題在哪裏,還是我使用了一個不好的解決方案?非常感謝您的幫助。
我的代碼
ResponsesViewController.h
@interface ResponsesViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *responses;
@property (nonatomic, strong) PFObject *selectedResponse;
@end
ResponsesViewController.m
@interface ResponsesViewController()
@end
@implementation ResponsesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.tableView reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//getting the objects from parse.com
PFQuery *query = [PFQuery queryWithClassName:@"smajlici"];
[query whereKey:@"recieverFacebookiD" equalTo:[[PFUser currentUser] objectForKey:@"fbId"]];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"current user is %@", error);
} else {
//adding objects from server into MutableArray
self.responses = [objects mutableCopy];
[self.tableView reloadData];
}
}];
}
//------------------------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 69;
}
//------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//------------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.responses count];
}
//------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"responseCell" forIndexPath:indexPath];
PFObject *object = [self.responses objectAtIndex:indexPath.row];
cell.textLabel.text = [object objectForKey:@"senderFbName"];
return cell;
}
//------------------------------------------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedResponse = [self.responses objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"detailResponse" sender:self];
}
//------------------------------------------
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//sending object to DetailResponsesVC
if ([segue.identifier isEqualToString:@"detailResponse"]) {
DetailResponsesViewController *controller = (DetailResponsesViewController *)segue.destinationViewController;
controller.selectedResponse = self.selectedResponse;
}
}
@end
DetailResponsesViewController.h
@interface DetailResponsesViewController : UIViewController
@property (nonatomic, strong) PFObject *selectedResponse;
- (IBAction)backButton:(id)sender;
@end
DetailResponsesViewCo ntroller.m
@interface DetailResponsesViewController()
@end
@implementation DetailResponsesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = YES;
}
- (IBAction)backButton:(id)sender {
//selectedresponse is an object sent to this viewController using SEGUE
//from "didselectrowAtIndexPath" ResponsesViewController
[self.selectedResponse deleteInBackground];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
您可以添加自定義委託重載和刪除我不知道數據 – BHASKAR 2014-09-02 10:41:05
,但它看起來像你從後臺線程(findObjectsInBackgroundWithBlock)重裝表可能是你可以嘗試在運行它主線程。 – Krrish 2014-09-02 10:44:20
嘗試重新加載主線程'[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];' – codeIgnitor 2014-09-02 10:46:17