2012-08-31 43 views
1

我有一个名为FINALORDER的db表,它填充了一些数据,然后显示在tableview中。当用户点击alertview时从sqlite数据库中删除一条记录

我插入了didSelectRowAtIndexPath内的alertview ..

alert=[[UIAlertView alloc]initWithTitle:@"Cancel Order" message:@"Do you want to cancel the order" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:@"Ok",nil]; 
    [alert show]; 
    [alert release]; 

和委托方法如下:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
const char *dbpath = [databasePath UTF8String]; 
sqlite3_stmt *statement; 
if(buttonIndex==1) 
{ 
    NSString *deleteSQL = @"DELETE FROM FINALORDER WHERE itemname=?"; 

    if (sqlite3_prepare_v2(database, [deleteSQL UTF8String], -1, &statement, NULL) != SQLITE_OK) 
     NSLog(@"%s error preparing %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_bind_text(statement, 1, [itemname UTF8String], -1, NULL) != SQLITE_OK) 
     NSLog(@"%s error binding %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_step(statement) != SQLITE_DONE) 
     NSLog(@"%s error executing %s", __FUNCTION__, sqlite3_errmsg(database)); 
    if (sqlite3_finalize(statement) != SQLITE_OK) 
     NSLog(@"%s error finalizing %s", __FUNCTION__, sqlite3_errmsg(database)); 


} 
} 

我怎么得到它可以在取代项目名称问号的地方?请注意,我的数据库包含以下字段itemname,quantity,totalcost

+1

你为什么不使用CoreData?你知道这是一个下面的SQLight数据库。它使用起来也更容易,并且最终编写的代码更少以管理数据。 – AppHandwerker

+0

我已经在诅咒自己了.. ..但是,我已经走了很远,我的项目回头看起来似乎不是一个选项.. :-( – BoredToDeath

+0

我会争辩说,它总是一个选项,它会让你生活在从长远来看,这是非常容易的,至少你已经定义了数据模型,所以在CoreData中实现它应该是一件轻而易举的事情,这里有很多示例供你跟随,以及来自Apple的一些很好的示例代码 – AppHandwerker

回答

1

in didSelectRowAtIndexPath 将名称存储在类变量中,然后使用它。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
NSArray *names = [cell.textLabel.text componentsSeparatedByString:@" "]; 
NSString *temp; 
for (int i=0; i<[names count]-2; i++) 
{ 
    if (!temp) 
    { 
     temp = [names objectAtIndex:i]; 
    } 
    else 
    { 
     temp = [NSString stringWithFormat:@"%@ %@",temp, [names objectAtIndex:i]]; 
    } 
} 
self.itemName = temp; 

,并用它

NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM FINALORDER WHERE itemname=%@",self.itemName]; 

希望它帮助。快乐编码:)

+0

你的答案似乎是正确的,但我没有一个arrayNameToDisplayData..i只有记录存储在数据库中的某处..我使用numericIds数组访问它们..我希望你下站在我的意思 – BoredToDeath

+0

无论你显示数据的方式,必须有一个索引路径用于cellForRowAtIndexPath方法中的每个单元格。所以在arrayNameToDisplayData的情况下,你可以直接访问你的数据库。 –

+0

看看我的cellForrowat..http://pastie.org/4630043 – BoredToDeath

1

你可以做什么@AnshukGarg建议或者我会这样做。您可以创建自己的自定义类来处理UIAlertView。

例如:

  DataAlertThing.h 

      #import <Foundation/Foundation.h> 

      @protocol DataAlertThingDelegate{ 
      @required 

       -(void)shouldDeleteItem:(ItemType*)itemName; 
      } 

      @interface DataAlertThing : NSObject <UIAlertViewDelegate> 
      @property (nonatomic, weak)id delegate; 
      @property (nonatomic, strong) id itemName; 

      -(id)initWithItemName:(ItemType*)myItemName; 
     @end 

    DataAlertThing.m 

    #import "DataAlertThing.h" 

    @implementation DataAlertThing 



-(id)initWithItemName:(ItemType*)myItemName{ 
    self = [super init]; 
    if (self !=nil{ 
    self.itemName = myItemName; 
    UIAlertView *alert = [UIalertVIew alloc]init...delegate:self..] 
    [alert show]; 
} 
return self; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(buttonIndex==1) 
{ 

    [delegate shouldDeleteItem:self.itemName]; 

} 
} 
从上面你得到足够的想法,以什么我建议的

希望 - 你封装你的警报逻辑在自己的自定义类。你也可以在这一点上,而不是使用一个块,但我认为一个例子就够了今天:)

请注意,这ARC还利用现代约定,所以你不需要右边的@synthesize

在您初始化警报视图的现有代码中。改为初始化此自定义类。

whatever = [[DataAlertThing alloc]initWithItemName:myItemName]; 
[whatever setDelegate:self]; 

以及与此实现以下

-(void)shouldDeleteItem:(ItemType*)itemName{ 
//do what you need here to delete the record 

} 
+0

我明白你的意思。 。但你可以建议一些比这个..我真的不知道你将如何传递你的myItemName ..注意:我有我的数据库中的3个字段..我用一个名为numericIds的NSMutableArray显示数据..我希望你明白我的意思.. – BoredToDeath

+0

然后制作“ItemType”ID,然后传递它一个NSMutableArray,毕竟它不关心它传入或传出什么,因为DataAlertThing不会对数据做任何事情。因此它可以告诉类创建它做什么。然后,需要在现有类(当前创建UIAlertView的类)中采用DataAlertThingDelegate协议,以便它可以响应回调并使用删除记录数据通过sed它。 – AppHandwerker

+0

我已更新我的答案以反映更改并修复一些相当明显的遗漏。 – AppHandwerker