首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在视图类中编写了一个tableView,但是它不能显示dataSource

我在视图类中编写了一个tableView,但是它不能显示dataSource
EN

Stack Overflow用户
提问于 2016-11-05 04:12:53
回答 2查看 81关注 0票数 0

导入"YBLeftView.h“

代码语言:javascript
复制
@interface YBLeftView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *myLeftView;
@property(nonatomic,strong)NSMutableArray *tableDataArray;
@property(nonatomic,strong)UITableView *tableView;
@end

//属性初始化;汇出

代码语言:javascript
复制
@implementation YBLeftView

-(instancetype)init
{
    UIView *myLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)];
    myLeftView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];

    //tableView
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 114, myLeftView.bounds.size.width, myLeftView.bounds.size.height-164) style:UITableViewStylePlain];
    [myLeftView addSubview:tableView];
    self.tableView=tableView;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
    tableView.separatorStyle=NO;
    //data
    NSMutableArray *tableDataArray=[[NSMutableArray alloc]init];
    NSArray *dataArray=@[@"首页",@"日常心理学",@"用户推荐日报",@"电影日报",@"不许无聊",@"设计日报",@"大公司日报",@"财经日报",@"互联网安全",@"开始游戏",@"音乐日报",@"动漫日报",@"体育日报"];
    self.tableDataArray=tableDataArray;
    [self.tableDataArray addObjectsFromArray:dataArray];


    return myLeftView;
}

#实用化标记-表视图数据源

//这一行工作正常,它记录tableDataArray.count的计数,tableView返回计数的数量

代码语言:javascript
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%lu",(unsigned long)self.tableDataArray.count);
    return self.tableDataArray.count;
}

//这一行不工作,甚至

NSLog(@"%@",_tableDataArray[indexPath.row]);

这个密码。我添加了dataSource和委托,为什么会发生这种情况?

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID=@"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSLog(@"%@",_tableDataArray[indexPath.row]);
//    cell.textLabel.text=_tableDataArray[indexPath.row];
    cell.textLabel.text=@"22";
    cell.imageView.image=[UIImage imageNamed:@"Menu_Follow"];
    return cell;
}
@end

代码执行

EN

回答 2

Stack Overflow用户

发布于 2016-11-05 08:06:46

您可以尝试在左视图中添加表格视图的代码,如下所示:

代码语言:javascript
复制
UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 114,myLeftView.bounds.size.width, myLeftView.bounds.size.height-164) style:UITableViewStylePlain];
    self.tableView=tableView;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [myLeftView addSubview:self.tableView];
    ...
    [self.view addSubview:myLeftView];

尝试代码,希望你得到结果。因为在添加视图后将所有属性赋予控制器可能会导致意外结果。因此,在将所有属性都提供给它之后,只需尝试将表视图添加到左侧视图中即可。然后在主视图中添加左视图。

这可能对你有帮助。

祝你好运。

票数 1
EN

Stack Overflow用户

发布于 2016-11-05 12:12:22

重写initWithFrame:方法的UIView。您不必在YBLeftView中声明另一个视图。相反,应该是这样:

代码语言:javascript
复制
@implementation YBLeftView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 114, myLeftView.bounds.size.width, myLeftView.bounds.size.height-164) style:UITableViewStylePlain];
    [self addSubview:self.tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
    tableView.separatorStyle=NO;
    //data
    self.tableDataArray=[[NSMutableArray alloc]init];
    NSArray *dataArray=@[@"首页",@"日常心理学",@"用户推荐日报",@"电影日报",@"不许无聊",@"设计日报",@"大公司日报",@"财经日报",@"互联网安全",@"开始游戏",@"音乐日报",@"动漫日报",@"体育日报"];

    [self.tableDataArray addObjectsFromArray:dataArray];

    return self;
}

然后,在初始化YBLeftView时,可以调用init方法,如下所示:

代码语言:javascript
复制
YBLeftView *leftView = [[YBLeftView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)]]; 

这种方式更灵活。

对于tableView问题,我认为您只是在将表视图添加到YBLeftView之后声明了数据数组。也许您可以尝试在添加/声明tableView之前声明它。因为那个时候,数据阵列的计数应该仍然是零,这就是为什么cellForRowAtIndexPath:没有被调用的原因。

或者,您可以尝试重新加载整个表,看看是否真的是这样。重新加载表就像:

代码语言:javascript
复制
[_tableView reloadData];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40434682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档