Ios 填充UITableView:空表。为什么?
我会简单解释 我有两个类在它们之间传递数据。在一个类(我称之为类1)中,我有一个警报控制器,在那里我可以得到一个标题和一个html文本Ios 填充UITableView:空表。为什么?,ios,objective-c,uitableview,uiwebview,Ios,Objective C,Uitableview,Uiwebview,我会简单解释 我有两个类在它们之间传递数据。在一个类(我称之为类1)中,我有一个警报控制器,在那里我可以得到一个标题和一个html文本 - (IBAction)downloadPage:(id)sender { NSString *html = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"]; UIAlertController
- (IBAction)downloadPage:(id)sender {
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:
@"document.body.innerHTML"];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Inserisci il titolo"
message: @""
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Titolo";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Do you calculation and UI refresh on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSArray * textfields = alertController.textFields;
UITextField * title = textfields[0];
InfoHtml *newFeed = [[InfoHtml alloc] init];
newFeed.title = title.text;
newFeed.html = html;
[self.arrayHTML addObject:newFeed];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.arrayHTML];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"html"]; //salvo in userdefault
[[NSUserDefaults standardUserDefaults]synchronize];
});
}]];
其中,InfoHtml是这种类型的类模型:
@interface InfoHtml : NSObject <NSCoding>
@property (nonatomic,strong) NSString *title;
@property (nonatomic,strong) NSString *html;
@end
我已将数据提取插入到VIEWWILLEXPEND方法中,因为此控制器连接到一个控制器选项卡栏,因此每次我打开此控制器时,您都必须检查是否有任何新数据
然后,在cellForRowAtIndexPath中,我有:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
InfoHtml *infoToShow = [self.arrayHTML objectAtIndex:indexPath.row];
cell.textLabel.text = infoToShow.title; //setto titolo
return cell;
}
我不明白为什么UITableView仍然为空且不填充。数据之所以存在,是因为
NSData*Data=[[NSUserDefaults standardUserDefaults]objectForKey:@“html”]
它给我的不是nil。您设置了表视图的委托和数据源了吗?@AbdAl rhmanTaherBadary yepDid是否覆盖numberofrowsinssection
以返回数组计数?@GIJOW yep,(NSInteger)tableView:(UITableView*)tableView numberofrowsinssection:(NSInteger)section{return self.arrayHTML.count;}
接下来,我将使用此行中的断点进行调试,以查看委托是否正常工作,以及数组是否返回非零的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
InfoHtml *infoToShow = [self.arrayHTML objectAtIndex:indexPath.row];
cell.textLabel.text = infoToShow.title; //setto titolo
return cell;
}