如何在 UITableView ios 中显示数据
Posted
技术标签:
【中文标题】如何在 UITableView ios 中显示数据【英文标题】:How to show Data in UITableView ios 【发布时间】:2013-11-25 13:15:46 【问题描述】:开发一个 iPAD 应用程序,我试图在其中填充表格,这就是我正在做的事情: 在视图确实加载时,我正在创建两个字典对象并将对象添加为 Pdf 和 Excel。
- (void)viewDidLoad
[super viewDidLoad];
arrDocuments = [[NSMutableArray alloc] init];
NSArray *arr1 = [NSArray arrayWithObjects:@"PDF", nil];
NSDictionary *pdf = [NSDictionary dictionaryWithObjects:arr1 forKeys:nil];
NSArray *arr2 = [NSArray arrayWithObjects:@"Excel", nil];
NSDictionary *excel = [NSDictionary dictionaryWithObjects:arr2 forKeys:nil];
[arrDocuments addObject:pdf];
[arrDocuments addObject:excel];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [arrDocuments count];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
//return [arrReceipes count];
NSDictionary *dictionary = [arrDocuments objectAtIndex:section];
NSArray *array = [dictionary objectForKey:[self.sortedKeys objectAtIndex:section]];
return [array count];
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *MyIdentifier = @"MyIdentifier";
UITableView *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell ==nil)
cell = [[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:MyIdentifier];
return cell;
有了这个实现,我看不到表格视图。它进入主方法并向我显示 SIGABRT 消息。我哪里出错了??
【问题讨论】:
在 cellForRowAtIndexPath 添加:cell.textLabel.text = ... 您实际上是在哪里创建表格视图的? @llario,智能没有显示“cell.textLable.text”,当我写这个时,它显示的 textLable 没有被识别.. 发布任何 NSLog in 方法并检查应用程序崩溃的位置.. 这个 self.sortedkeys 是什么? 【参考方案1】:在创建 NSDictionary
时,您传递了 nil 来代替键数组,
NSDictionary *pdf = [NSDictionary dictionaryWithObjects:arr1 forKeys:nil];
您应该为字典提供适当的对象和键。如您在其方法签名中所见,此方法期望在数组中给出对象和键。
+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
【讨论】:
以上是关于如何在 UITableView ios 中显示数据的主要内容,如果未能解决你的问题,请参考以下文章
iOS - Swift Google AdMob SDK 如何在 UITableView 的重复项目中显示不同的广告?
如何以普通样式隐藏在 UITableView 中显示的空 UITableViewCells - IOS?
如何在 swift iOS 中的 UIAlertcontroller 中显示 UITableview?