如何将更新的数据库(我的书签数据)从一个视图传递到另一个在表视图中填充的选项卡项的视图?



请指导我传递数据。我的故事板包含两个选项卡。在第一个视图中,我在导航栏中按选择器放置了两个按钮,用于添加和删除书签中的对象。在这里之前这不是问题。当我更改选项卡以在表视图中显示更新的书签数据库时,我会在行中看到重复的对象。我使用

- (void)viewWillAppear:(BOOL)animated

[tableview reloadData];

但它仍然显示了重复的对象。这是我显示更新数据的第二类代码

@implementation BookmarkViewController
@synthesize data , tableData , tblView ;
- (void)viewDidLoad{
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    sql = [[SQLiteManager alloc]initWithDatabaseNamed:@"datadic.db"];
    bookmarkQuery = @"SELECT EN FROM table WHERE BOOKMARK IS 1";
    res = [sql getRowsForQuery:bookmarkQuery];
    tableData = [[NSMutableArray alloc]initWithArray:[res valueForKey:@"EN"]];
    NSLog(@"%@" , tableData);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    indexRow = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    tblView = [[UITableView alloc]init];
    static NSString *myIdentifier = @"CELL";
    myCell = [tblView dequeueReusableCellWithIdentifier:myIdentifier];

    if (myCell == nil) {

        myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: myIdentifier];
    }
    [tblView reloadData];
    myCell.textLabel.textColor = [UIColor blueColor];
    myCell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return myCell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableData count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableData removeObjectAtIndex:indexPath.row];
    [tblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                   withRowAnimation:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
@end

问题很可能是每次调用[cellForRowAtIndexPath]时都在实例化UITableView的备用副本。您在此副本上同时调用[reloadData]和[deleteRowsAtIndexPaths],这不会起任何作用。

试试这个修改后的代码(我假设BookmarkViewController是UITableViewController的子类):

@implementation BookmarkViewController
@synthesize data , tableData , tblView ;
- (void)viewDidLoad{
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    sql = [[SQLiteManager alloc]initWithDatabaseNamed:@"datadic.db"];
    bookmarkQuery = @"SELECT EN FROM table WHERE BOOKMARK IS 1";
    res = [sql getRowsForQuery:bookmarkQuery];
    tableData = [[NSMutableArray alloc]initWithArray:[res valueForKey:@"EN"]];
    NSLog(@"%@" , tableData);
    [tblView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    indexRow = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    static NSString *myIdentifier = @"CELL";
    myCell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

    if (myCell == nil) {

        myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: myIdentifier];
    }
    myCell.textLabel.textColor = [UIColor blueColor];
    myCell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return myCell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableData count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableData removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                   withRowAnimation:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
@end

最新更新