1

我是 xcode 的初学者,对此我知之甚少。这是我的代码。我正在为 iOS 6.0 做一个简单的帐户管理程序。我需要从表格视图中选择要编辑/删除数据的行,然后才能更新/删除它。我不知道如何编写这些代码,我需要有经验的程序员的帮助。我的项目:http ://www.mediafire.com/?95l8pwzu3c62ts0

#import "FlipsideViewController.h"

@interface FlipsideViewController ()

@end
@implementation FlipsideViewController

@synthesize entries;
- (void)viewDidLoad
{
    self.label.text=self.strUsername;
    entries = [[NSMutableArray alloc]init];
    [self openDB];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM SUMMARY8 WHERE theuser = '%s'",[self.label.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(account, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW){
            char *field1=(char *) sqlite3_column_text(statement, 0 );
            NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];

        char *field2=(char *) sqlite3_column_text(statement, 1 );
          NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];

            char *field3=(char *) sqlite3_column_text(statement, 2 );
            NSString *field3Str = [[NSString alloc]initWithUTF8String:field3];

            char *field4=(char *) sqlite3_column_text(statement, 3 );
            NSString *field4Str = [[NSString alloc]initWithUTF8String:field4];

            NSString *str = [[NSString alloc]initWithFormat:@"%@ - %@ - %@", field1Str,field3Str,field4Str];
            [entries addObject:str];
        }
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"account.sql"];
}
-(void) openDB
{
    if (sqlite3_open([[self filePath] UTF8String], &account) !=SQLITE_OK){
        sqlite3_close(account);
        NSAssert(0,@"Database failed to open");

    }
    else
    {
        NSLog(@"database opened");
    }
}

- (IBAction)addaccount:(id)sender {
    MainViewController *FVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
    FVC.strUsername=self.strUsername;

    [self presentViewController:FVC animated:YES completion:nil];
   // FVC.label.text=self.label.text;
}

- (IBAction)btnEdit:(id)sender {
}

- (IBAction)btnDelete:(id)sender {
    [self tableView:<#(UITableView *)#> cellForRowAtIndexPath:<#(NSIndexPath *)#>];
}

#pragma mark - Actions

- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

-(NSInteger) numberOfSectionsInTableView: (UITableView *)tableView
{
    return 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{
    NSString *myTitle = [[NSString alloc]initWithFormat:@"Name - Username - Password"];
    return myTitle;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      NSLog(@"%d", [entries count]);
    return [entries count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//NSLog(@"test");
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *selectedRow = [entries objectAtIndex:indexPath.row];
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM SUMMARY8 WHERE name = '%@'",selectedRow];

    char *err;
    if (sqlite3_exec(account, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK){
        sqlite3_close(account);
        NSAssert(0, @"Could not delete row");

    }
    else {
        NSLog(@"row deleted");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was deleted successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

@end

它是否正确?抱歉,我绝对没有 Xcode 的背景。很抱歉打扰了..

4

1 回答 1

0

删除使用这个:

例子:strUpdate = [NSString stringWithFormat:@"DELETE FROM SUMMARY8 WHERE theuser = '%s'",[self.label.text UTF8String]];

更新使用这个:

示例(更新状态):strQuery = [NSString stringWithFormat:@"UPDATE SUMMARY8 SET STATUS='1' WHERE theuser = '%s'",[self.label.text UTF8String]];

希望这可以帮助。

编辑:使用这种方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

Example: NSString *selectedRow = [entries objectAtIndex:indexPath.row];
}

这种方法基本上显示了该行表的数据。使用该数据进行操作。举个例子..根据你的情况调整它......

编辑2:

例如:

UITableView *table = [[UITableView alloc] init];
[self tableView:table didSelectRowAtIndexPath:index];

其中 index 是您的商品编号。

于 2013-01-18T08:36:56.183 回答