2016-11-05 1 views
0

輸入 "YBLeftView.h"

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

財産の//初期化を表示することはできません。私は私のビュークラスでのtableViewを書き、それがデータソース

@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 *[email protected][@"首页",@"日常心理学",@"用户推荐日报",@"电影日报",@"不许无聊",@"设计日报",@"大公司日报",@"财经日报",@"互联网安全",@"开始游戏",@"音乐日报",@"动漫日报",@"体育日报"]; 
    self.tableDataArray=tableDataArray; 
    [self.tableDataArray addObjectsFromArray:dataArray]; 


    return myLeftView; 
} 
を省略

#pragma mark - テーブルビューデータソース

//この行が正常に動作し、それも

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

、この行は動作しません数

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

//の数を返しtableDataArray.countとのtableViewのカウント数を記録しますこのコード。私はdataSourceを追加してデリゲートしました、なぜこれが起こりますか?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"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]; 
    [email protected]"22"; 
    cell.imageView.image=[UIImage imageNamed:@"Menu_Follow"]; 
    return cell; 
} 
@end 

code excuted

+0

あなたは何をしようとしていますかreloadData –

+0

あなたは何をしようとしていますか?アーカイブしようとしていることを教えてください。 –

+0

この行を置き換えてみてください 'UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; ' –

答えて

1

あなたは以下のように左側のビューでテーブルビューを追加するためのコードを試すことができます。

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


は、あなたが結果を得ることを願っていたコードを試してみてください。すべてのプロパティをビューに追加した後にコントローラに与えると、予期しない結果が生じる可能性があります。だから、すべてのプロパティを与えた後、左側のビューでテーブルビューを追加しようとするだけです。メインビューに左のビューを追加します。
これはあなたを助けるかもしれません。
運が良かったです。

+0

[self.view addSubview:myLeftView]; 自己はビューですが、ビューのプロパティはありません – yangOne

0

UIViewinitWithFrame:メソッドをオーバーライドします。あなたは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 *[email protected][@"首页",@"日常心理学",@"用户推荐日报",@"电影日报",@"不许无聊",@"设计日报",@"大公司日报",@"财经日报",@"互联网安全",@"开始游戏",@"音乐日报",@"动漫日报",@"体育日报"]; 

    [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を追加した直後にデータ配列を宣言したと思います。おそらく、あなたがそれを宣言する前に、あなたが/ tableViewを宣言することができます。その時間のため、あなたのdataArrayのカウントはゼロでなければならないので、cellForRowAtIndexPath:が呼び出されなかったのです。

また、テーブル全体を再読み込みして実際に大文字かどうかを確認することもできます。テーブルを再読み込みすると、次のようになります。

[_tableView reloadData]; 
関連する問題