gpt4 book ai didi

ios - 我在 View 类中编写了一个tableView,但它无法显示数据源

转载 作者:行者123 更新时间:2023-11-29 00:36:41 24 4
gpt4 key购买 nike

import "YBLeftView.h"

@interface YBLeftView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *myLeftView;
@property(nonatomic,strong)NSMutableArray *tableDataArray;
@property(nonatomic,strong)UITableView *tableView;
@end

//初始化属性;略

@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;
}

#pragma mark - TableView 数据源

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%lu",(unsigned long)self.tableDataArray.count);
return self.tableDataArray.count;
}

//这条线不工作,即使

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

这段代码。我已经添加了数据源和委托(delegate),为什么会这样?

- (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

code excuted

最佳答案

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

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];


尝试代码希望你得到结果。因为在将所有属性添加到 View 中后将所有属性赋予 Controller 可能会导致意外结果。因此,在为表格 View 提供所有属性后,尝试将其添加到左 View 中。然后在主视图中添加您的左 View 。
这可能对您有所帮助。
祝你好运。

关于ios - 我在 View 类中编写了一个tableView,但它无法显示数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40434682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com