Ios 插入到UITableView中
我试图用文件的内容填充UITableView。或者至少这是我的最终目标,但对于测试,我只想插入字符串来测试它。我有一个读写按钮连接到我的ViewController。h作为@property和iAction来实现press,我有一个UITableView属性 这是我对读写按钮的实现Ios 插入到UITableView中,ios,objective-c,Ios,Objective C,我试图用文件的内容填充UITableView。或者至少这是我的最终目标,但对于测试,我只想插入字符串来测试它。我有一个读写按钮连接到我的ViewController。h作为@property和iAction来实现press,我有一个UITableView属性 这是我对读写按钮的实现 - (IBAction)readtofile:(id)sender { [_readview beginUpdates]; NSString *docPath = [NSHomeDirectory(
- (IBAction)readtofile:(id)sender {
[_readview beginUpdates];
NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/TestFile.txt"];
NSString* data = [[NSString alloc] initWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:NULL];
NSArray* arr = [NSArray arrayWithObjects:data,data,data, nil];
[_readview insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
[_readview endUpdates];
}
- (IBAction)writetofile:(id)sender {
NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/TestFile.txt"];
NSString* data = @"hello world";
[data writeToFile:docPath
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL];
}
编辑:这是更新的代码和瓦迪安的答案。但是UITableView仍然没有被填充
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *readview;
@property (strong) NSMutableArray<NSString *> *items;
- (IBAction)readFromFile:(id)sender;
- (IBAction)writeToFile:(id)sender;
@end
cellForRowAtIndexPath方法内部崩溃。List[2558:545971]***-[UITableView\u dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:,
中的断言失败,因此我不确定如何正确实现该方法。
这是我的UI布局。
很抱歉,这两种方法几乎任何一行都无法使用 首先,数据源需要一个
NSMutableArray
@property (strong) NSMutableArray<NSString *> *items;
让我们从save方法开始。将项
序列化为属性列表并将其保存到磁盘。在iOS中,您必须使用NSFileManager
API来获取(当前)文档文件夹
- (IBAction)writeToFile:(id)sender {
NSURL *docURL = [[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil] URLByAppendingPathComponent:@"TestFile.plist"];
NSError *error;
NSData *propertyListData = [NSPropertyListSerialization dataWithPropertyList:items
format:NSPropertyListXMLFormat_v1_0
options:0
error:&error];
if (error) {
NSLog(@"%@", error);
} else {
[propertyListData writeToURL:docURL atomically:YES];
}
}
read方法按相反顺序执行相同操作:读取数据,将其反序列化到NSMutableArray
,并将结果分配给数据源数组。然后重新加载表视图
- (IBAction)readFromFile:(id)sender {
NSURL *docURL = [[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil] URLByAppendingPathComponent:@"TestFile.plist"];
NSData* propertyListData = [[NSData alloc] initWithContentsOfURL: docURL];
NSError *error;
self.items = (NSMutableArray *)[NSPropertyListSerialization propertyListWithData: propertyListData
options:NSPropertyListMutableContainersAndLeaves
format:NULL
error:nil];
if (error) {
NSLog(@"%@", error);
} else {
[_readview reloadData];
}
}
它崩溃导致tableView数据源与不同步insertion@MarekH我不确定我是否理解。它是有文档记录的。读取,所以它看起来读写都很好,但它没有填充UITableView*\u readview。我不知道调用reloadData如何将数据插入tableview。您必须调整table view数据源方法(
numberOfRowsInSection
和cellForRowAtIndexPath
)并且您必须将表视图的数据源连接到视图控制器(控件从表视图拖动到控制器,然后选择dataSource
)。是的,reloadData
插入行。编辑了我的问题,实现了numberOfRowsInSection和cellForRowAtIndexPath。但是,我不确定将表视图的数据连接到视图控制器是什么意思。您的意思是将其作为属性连接吗?您必须将接口更改为@interface ViewController:UIViewController
并且您还可以在代码中设置代理,在viewdiload
addself.readview.dataSource=self
中,我编辑了问题以插入初始代码。如果没有此代码,其他读者将无法理解该问题。
- (IBAction)writeToFile:(id)sender {
NSURL *docURL = [[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil] URLByAppendingPathComponent:@"TestFile.plist"];
NSError *error;
NSData *propertyListData = [NSPropertyListSerialization dataWithPropertyList:items
format:NSPropertyListXMLFormat_v1_0
options:0
error:&error];
if (error) {
NSLog(@"%@", error);
} else {
[propertyListData writeToURL:docURL atomically:YES];
}
}
- (IBAction)readFromFile:(id)sender {
NSURL *docURL = [[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil] URLByAppendingPathComponent:@"TestFile.plist"];
NSData* propertyListData = [[NSData alloc] initWithContentsOfURL: docURL];
NSError *error;
self.items = (NSMutableArray *)[NSPropertyListSerialization propertyListWithData: propertyListData
options:NSPropertyListMutableContainersAndLeaves
format:NULL
error:nil];
if (error) {
NSLog(@"%@", error);
} else {
[_readview reloadData];
}
}