2013-10-03 55 views
0

下面是我實現的cellForRowAtIndexPath方法如何從源碼刪除特定的細胞和行IOS SDK

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *[email protected]"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 *[email protected]"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 

如何得到這個工作?任何幫助將aprreciated

+0

使用NSManagedobjectContext的deleteObject方法。 –

+0

您使用的是Sqlite還是核心數據? – Ganapathy

+0

我使用Sqlite – Vibhs

回答

0

嘗試這樣的。

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

我使用Sqlite DB – Vibhs