【发布时间】:2012-04-07 06:06:07
【问题描述】:
我正在制作聊天应用程序并从 SQLite 数据库读取消息数据。单击“发送”按钮后,我想用新消息重新加载 TableView 数据。 我的按钮的事件:
[_sendButton addTarget:self action:@selector(saveMessage:) forControlEvents:UIControlEventTouchUpInside];
我想我需要使用-(void)viewDidAppear:(BOOL)animated,但它不起作用(或者我做错了什么)。
[_tableView reloadData];
-(void)viewDidAppear:(BOOL)animated {
sqlite3 *database;
databaseName = @"Messenges.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
messenges = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from messenges";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *dbMessageText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
Message *messege = [[Message alloc] initWithName:dbMessageText];
[messenges addObject:messege];
[messege release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);}
编辑 这是我向 TableView 添加新行的方法。单击“发送”按钮后必须立即添加新行。
-(IBAction)insertRows:(id)sender {
MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
[messenges insertObject:[[NSString alloc] initWithFormat:@"%@", _textField.text] atIndex:appDelegate.messenges.count+1];
NSArray *insertIndexPaths = [NSArray arrayWithObject: [NSIndexPath indexPathForRow:1 inSection:1]];
UITableView *tV = (UITableView *)self.tableView;
[tV beginUpdates];
[tV insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tV endUpdates];}
但是这段代码给了我错误:'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.'
我该如何解决?
编辑2
好的。我只有 1 个部分。 numberOfSectionsInTableView: 不需要更正。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
NSUInteger numberOfRowsInSection = appDelegate.messenges.count; //add new variable to what rows will be +1
if (self.editing) {
numberOfRowsInSection++;
}
return numberOfRowsInSection;}
但我仍然有同样的错误。
编辑#3
让我们看看。我将insertRows 更改为:
-(IBAction)insertRows:(id)sender {
MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
[self.tableView beginUpdates];
self.tableView.editing = YES;
[messenges insertObject:[[NSString alloc] initWithFormat:@"%@", _textField.text] atIndex:appDelegate.messenges.count+1];
NSArray *insertIndexPaths = [NSArray arrayWithObject: [NSIndexPath indexPathForRow:appDelegate.messenges.count+1 inSection:1]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
还有numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
NSUInteger numberOfRowsInSection = appDelegate.messenges.count;
if (self.tableView.editing) {
numberOfRowsInSection++;
}
NSLog(@"%i",numberOfRowsInSection);
return numberOfRowsInSection;
}
但我收到了一个错误 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]' 。然后我将if (self.tableView.editing) 更正为:
if (self.tableView.editing) {
MDAppDelegate *someNewDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
numberOfRowsInSection = someNewDelegate.messenges.count+1; //or .count
}
但是遇到了关于“NSRangeException”的相同错误。
【问题讨论】:
-
查看Animate the insertion of a new section in UITableVIew。它还讨论了在现有部分中以动画方式插入行。
-
我只是想指出,您拼写了我认为应该是 message 的内容,三种不同的方式:message、messenge 和 messege。我认为只有其中一个是正确的。如果这些应该是四个不同的词,我认为你需要一个不同的命名方案。
-
@Sam 我对您的链接有一个问题。这些方法有能力在点击“发送”按钮后实时添加新行吗?以及在 iPhone 的“消息”中?
-
@RomanHouse 是的,您可以随时实时更新 tableview 行。诀窍是在调用
insertRowsAtIndexPaths:withRowAnimation:方法时更新您用来显示tableview 的任何数据模型。请注意,即使批量更新,您也可以一一插入它们。还记得在beginUpdates和endUpdates之间调用。 -
@Sam Hmm.. 我不明白如何实时添加新行。使用编辑模式,这不是问题。但是如果不使用编辑模式并且我在文本字段中有一些值需要在 TableView 中添加,我应该怎么做?
标签: objective-c ios sqlite tableview