导入"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;
}#实用化标记-表视图数据源
//这一行工作正常,它记录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]);
这个密码。我添加了dataSource和委托,为什么会发生这种情况?
- (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发布于 2016-11-05 08:06:46
您可以尝试在左视图中添加表格视图的代码,如下所示:
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];尝试代码,希望你得到结果。因为在添加视图后将所有属性赋予控制器可能会导致意外结果。因此,在将所有属性都提供给它之后,只需尝试将表视图添加到左侧视图中即可。然后在主视图中添加左视图。
这可能对你有帮助。
祝你好运。
发布于 2016-11-05 12:12:22
重写initWithFrame:方法的UIView。您不必在YBLeftView中声明另一个视图。相反,应该是这样:
@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方法,如下所示:
YBLeftView *leftView = [[YBLeftView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.height)]]; 这种方式更灵活。
对于tableView问题,我认为您只是在将表视图添加到YBLeftView之后声明了数据数组。也许您可以尝试在添加/声明tableView之前声明它。因为那个时候,数据阵列的计数应该仍然是零,这就是为什么cellForRowAtIndexPath:没有被调用的原因。
或者,您可以尝试重新加载整个表,看看是否真的是这样。重新加载表就像:
[_tableView reloadData];https://stackoverflow.com/questions/40434682
复制相似问题