Ios tableView数据未显示
我遇到了一个问题。当我在tableViewController中实现以下代码时,我没有问题,并且它显示的扩展/收缩表非常好。但是,当我尝试将代码实现到viewController内的tableView中时,它不会显示。知道怎么回事吗?我在my.h文件中包含了tableview委托和数据源文件 以下是头文件:Ios tableView数据未显示,ios,Ios,我遇到了一个问题。当我在tableViewController中实现以下代码时,我没有问题,并且它显示的扩展/收缩表非常好。但是,当我尝试将代码实现到viewController内的tableView中时,它不会显示。知道怎么回事吗?我在my.h文件中包含了tableview委托和数据源文件 以下是头文件: #import <UIKit/UIKit.h> @interface MainViewController : UIViewController <UITableVie
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *topItems;
NSMutableArray *subItems; // array of arrays
int currentExpandedIndex;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
#导入
@接口MainViewController:UIViewController
{
NSArray*顶级项目;
NSMUTABLEARRY*子项;//数组的数组
int currentExpandedIndex;
}
@属性(弱、非原子)IBUITableView*tableView;
@结束
这是.m文件
#import "MainViewController.h"
#define NUM_TOP_ITEMS 20
#define NUM_SUBITEMS 6
@interface MainViewController ()
@end
@implementation MainViewController
- (id)init {
self = [super init];
if (self) {
topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
subItems = [NSMutableArray new];
currentExpandedIndex = -1;
for (int i = 0; i < [topItems count]; i++) {
[subItems addObject:[self subItems]];
}
}
return self;
}
#pragma mark - Data generators
- (NSArray *)topLevelItems {
NSMutableArray *items = [NSMutableArray array];
for (int i = 0; i < NUM_TOP_ITEMS; i++) {
[items addObject:[NSString stringWithFormat:@"Item %d", i + 1]];
}
return items;
}
- (NSArray *)subItems {
NSMutableArray *items = [NSMutableArray array];
int numItems = arc4random() % NUM_SUBITEMS + 2;
for (int i = 0; i < numItems; i++) {
[items addObject:[NSString stringWithFormat:@"SubItem %d", i + 1]];
}
return items;
}
#pragma mark - View management
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [topItems count] + ((currentExpandedIndex > -1) ?
[[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *ParentCellIdentifier = @"ParentCell";
static NSString *ChildCellIdentifier = @"ChildCell";
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex +
[[subItems objectAtIndex:currentExpandedIndex] count];
UITableViewCell *cell;
if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
}
if (isChild) {
cell.detailTextLabel.text =
[[subItems objectAtIndex:currentExpandedIndex]
objectAtIndex:indexPath.row - currentExpandedIndex - 1];
}
else {
int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
: indexPath.row;
cell.textLabel.text = [topItems objectAtIndex:topIndex];
cell.detailTextLabel.text = @"";
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex +
[[subItems objectAtIndex:currentExpandedIndex] count];
if (isChild) {
NSLog(@"A child was tapped, do what you will with it");
return;
}
[self.tableView beginUpdates];
if (currentExpandedIndex == indexPath.row) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
else {
BOOL shouldCollapse = currentExpandedIndex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
[self expandItemAtIndex:currentExpandedIndex];
}
[self.tableView endUpdates];
}
- (void)expandItemAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [subItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:index inSection:0]
atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)collapseSubItemsAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
@end
#导入“MainViewController.h”
#定义数量顶部项目20
#定义NUM_子项6
@接口MainViewController()
@结束
@实现主视图控制器
-(id)init{
self=[super init];
如果(自我){
topItems=[[NSArray alloc]initWithArray:[self-topLevelItems]];
子项=[NSMutableArray new];
currentExpandedIndex=-1;
对于(int i=0;i<[topItems count];i++){
[subItems addObject:[self subItems]];
}
}
回归自我;
}
#pragma标记-数据生成器
-(NSArray*)顶级项目{
NSMutableArray*项=[NSMutableArray];
对于(int i=0;i-1)?
[[subItems objectAtIndex:currentExpandedIndex]计数]:0);
}
-(UITableView单元格*)表格视图:(UITableView*)表格视图单元格行索引路径:
(nsindepath*)indepath{
静态NSString*ParentCellIdentifier=@“ParentCell”;
静态NSString*ChildCellIdentifier=@“ChildCell”;
布尔是个孩子=
currentExpandedIndex>-1
&&indexPath.row>currentExpandedIndex
&&indexPath.row-1&&indexPath.row>currentExpandedIndex)
?indexath.row-[[subItems objectAtIndex:currentExpandedIndex]计数]
:indexath.row;
cell.textLabel.text=[topItems对象索引:topIndex];
cell.detailTextLabel.text=@”;
}
返回单元;
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
布尔是个孩子=
currentExpandedIndex>-1
&&indexPath.row>currentExpandedIndex
&&indexath.row-1;
如果(应该崩溃){
[自折叠Subitemsatindex:currentExpandedIndex];
}
currentExpandedIndex=(shouldCollapse&&indexath.row>currentExpandedIndex)?indexath.row-[[subItems objectAtIndex:currentExpandedIndex]count]:indexath.row;
[自扩展项索引:currentExpandedIndex];
}
[self.tableView endUpdates];
}
-(void)expandItemAtIndex:(int)索引{
NSMUTABLEARRY*INDExpath=[NSMUTABLEARRY new];
NSArray*currentSubItems=[子项对象索引:索引];
int insertPos=索引+1;
对于(int i=0;i<[currentSubItems count];i++){
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++片段:0];
}
[self.tableView insertRowsAtIndexPaths:indexPaths with rownanimation:uitableview rownanimationfade];
[self.tableView滚动箭头路径:
[NSIndexPath indexPathForRow:索引分区:0]
atScrollPosition:UITableViewScrollPositionTop动画:是];
}
-(无效)collapseSubItemsAtIndex:(int)索引{
NSMUTABLEARRY*INDExpath=[NSMUTABLEARRY new];
对于(int i=index+1;i您需要在viewDidLoad或XIB中设置委托和数据源
代码:
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
在控制器中:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
}
这似乎没有帮助。在我的appDelegate.m中,我的DidFinishLaunchingWith选项仅为“返回是”。此方法中是否还有其他内容?它应该在控制器中,而不是委托中。请参阅编辑。