UITableView 重新加载 - Xcode 5
Posted
技术标签:
【中文标题】UITableView 重新加载 - Xcode 5【英文标题】:UITableView reload - Xcode 5 【发布时间】:2014-01-07 21:03:52 【问题描述】:我正在使用嵌入在导航控制器中的 UITableView(带有表格单元)。应用启动时,会读取 SQLite3 数据库中的数据并将其添加到表格单元格中。
我制作了一个按钮(在导航栏上),它将引导用户进入一个他可以输入新数据的屏幕。当用户点击导航控制器自动添加的按钮“返回”时,就会发生“保存”。
当用户返回起始页时,数据不会被重新加载,因此新条目在重新启动应用程序之前是不可见的。
当数据返回视图时刷新数据的最佳方式是什么?
#import "lucidViewController.h"
#import "lucidCell.h"
@interface lucidViewController ()
@end
@implementation lucidViewController
@synthesize titleData,descrData,idKeyData;
- (void)viewDidLoad
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
[self openDB];
[self createTable:@"dreams" withField1:@"idKey" withField2:@"title" withField3:@"description"];
titleData = [[NSMutableArray alloc] init];
descrData = [[NSMutableArray alloc] init];
idKeyData = [[NSMutableArray alloc] init];
[self loadData];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (void) loadData
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM dreams"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(dataBase, [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];
[idKeyData addObject:field1Str];
[titleData addObject:field2Str];
[descrData addObject:field3Str];
- (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:@"lucid.sql"];
- (void) openDB
if (sqlite3_open([[self filePath] UTF8String], &dataBase) != SQLITE_OK)
sqlite3_close(dataBase);
NSAssert(0, @"Database kan niet geopend worden");
else
NSLog(@"Database is geopend!");
- (void) createTable:(NSString *)tableName withField1:(NSString *)field1 withField2:(NSString *)field2 withField3:(NSString *)field3
char *error;
NSString *sqlStatement = [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXISTS '%@'('%@' TEXT PRIMARY KEY, '%@' TEXT, '%@' TEXT);",tableName,field1,field2,field3];
if (sqlite3_exec(dataBase, [sqlStatement UTF8String], NULL, NULL, &error) != SQLITE_OK)
sqlite3_close(dataBase);
NSLog(@"Table kan niet aangemaakt worden..");
else
NSLog(@"Table aangemaakt!");
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [titleData count];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
return 0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
lucidCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!Cell)
Cell = [[lucidCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Cell.descrLabel.text = [self.descrData objectAtIndex:indexPath.row];
Cell.titleLabel.text = [self.titleData objectAtIndex:indexPath.row];
return Cell;
@end
此处添加新数据:
#import "newViewController.h"
@interface newViewController ()
@end
@implementation newViewController
@synthesize addNewDream;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[self openDB];
[self genRandStringLength:20];
[super viewDidLoad];
// Do any additional setup after loading the view.
- (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:@"lucid.sql"];
- (void) openDB
if (sqlite3_open([[self filePath] UTF8String], &dataBase) != SQLITE_OK)
sqlite3_close(dataBase);
NSAssert(0, @"Database kan niet geopend worden");
else
NSLog(@"Database is geopend!");
-(NSString *) genRandStringLength: (int) len
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++)
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
return randomString;
- (void) viewWillDisappear:(BOOL)animated
NSString *dreamText = addNewDream.text;
NSString *idKey = [self genRandStringLength:25];
NSDate *myDate = [NSDate date];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy, hh:mm"];
NSString *title = [df stringFromDate:myDate];
if (![dreamText isEqualToString:@""])
NSString *statement = [NSString stringWithFormat:@"INSERT INTO dreams ('idKey','title','description') VALUES ('%@','%@','%@')",idKey,title,dreamText];
char *error;
if (sqlite3_exec(dataBase, [statement UTF8String], NULL, NULL, &error) != SQLITE_OK)
sqlite3_close(dataBase);
NSAssert(0, @"Kan niet wegschrijven naar tabel");
else
NSLog(@"Table write succesvol!");
else
NSLog(@"Geen nieuwe droom ingevuld..");
【问题讨论】:
【参考方案1】:您可以像这样在 viewDidAppear 方法中刷新表格视图和数据模型:
-(void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
[self loadData];
[self.myTableView reload];
【讨论】:
太棒了!只有每次都将数据添加到我的数组中,所以我的列表中出现了重复的条目。我应该每次都清空数组还是有其他方法? 通过使数组为空来修复它。谢谢。以上是关于UITableView 重新加载 - Xcode 5的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 在 UiCollectionView 中仅重新加载一个单元格