簡體   English   中英

如何從iOS SDK中的SQLite中刪除特定的單元格和行

[英]How to delete particular cell and row from sqlite in ios sdk

這是我實現的cellforRowAtIndexPath方法

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier=@"myCell";
    CustomTableCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil)
    {
        cell=[[CustomTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
        DBObjectClass * info=[StudentInformationArr objectAtIndex:indexPath.row];
    cell.studIdLbl.text=[NSString stringWithFormat:@"%d",info.StudIDs];
    cell.studNameLbl.text=info.StudentName;
    cell.studEmailLbl.text=info.StudentEmail;
    cell.StudcontactLbl.text=[NSString stringWithFormat:@"%d",info.StudentContact];


    return cell;

}

其中DBObjectClass是NSOBject類模型類

這是我在TableView中顯示數據的方式

-(NSMutableArray*)StudentInformation;
{
    NSMutableArray *DBArray=[[NSMutableArray alloc]init];
    NSString *query=@"SELECT * FROM StudInfo ORDER BY studid";
    NSLog(@"Query is:%s",[query UTF8String]);
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW)
        {
            int  studID=sqlite3_column_int(statement, 0);
            char *studName=(char*)sqlite3_column_text(statement, 1);
            char *studEmail=(char*)sqlite3_column_text(statement, 2);
            int studContact= sqlite3_column_int(statement, 3);

            NSString *name=[[NSString alloc]initWithUTF8String:studName];
            NSString *email=[[NSString alloc]initWithUTF8String:studEmail];
            DBObjectClass *DbObj=[[DBObjectClass alloc]initWithSID:studID SName:name SEmail:email SContact:studContact];
            [DBArray addObject:DbObj];

        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(_database);
    return DBArray;
}

我試圖在這里實現刪除查詢

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

如何獲得這項工作?任何幫助將不勝感激

嘗試這樣。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    if (editingStyle == UITableViewCellEditingStyleDelete)
         {
         NSManagedObject * managedObject = [StudentInformationArr objectAtIndex:indexPath.row];

         NSError *error;   
            [[self getManagedObjectContext] deleteObject: managedObject];
            [[self getManagedObjectContext] save:&error];
        }
//before reload your tableView you have to fetch the data from your core data again. So that StudentInformationArr will get updated. Use something like 

    StudentInformationArr =[self fetchStudentInformationArray];

        [self.myTableView reloadData];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM