我在填充以编程方式创建的uitableview作为uicollectionview的子视图时遇到问题。
这是我的代码:
.h文件
#import <UIKit/UIKit.h>
@interface TheViewController : UICollectionViewController<UITableViewDataSource>
@property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
@end
.m文件
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *jsonParsingError = nil;
teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];
NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
teams = [[NSMutableArray alloc]init];
[teamsView addSubview:teamCell];
teamsView.delegate = self;
teamsView.dataSource = self;
for(int i =0;i<tmp.count;i++){
[teams addObject:[tmp objectAtIndex:i]];
}
plus.title = @"Done";
[self.view addSubview:teamsView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [teams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.name.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
NSLog(@"%d teams",[teams count]);
return cell;
}
我的tableview正确显示,当然要归功于此处未提及的其他功能,但子视图中没有行。 编译器打印出团队数量,因此它调用委托方法,但它不会填充子视图......
这有什么问题?
提前谢谢 达里奥答案 0 :(得分:2)
首先,您应该从viewDidLoad
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
[teamsView addSubview:teamCell];
然后确保保留teams
数组,只需将此行放入标题文件中:
@property (nonatomic, strong) NSMutableArray* teams;
然后使用self.teams
而不是teams
在viewDidLoad
方法结束时,您应致电[teamsView reloadData];
还可以更改表数据源方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// please make sure that you allocate required object here
cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// please make sure that you do have `name` property in your `TeamsNameCell` class
cell.name.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
NSLog(@"%d teams",[teams count]);
return cell;
}
请确保您的TeamsNameCell
班级有name property in the header
:
@property(nonatomic, strong) UILabel* name;
并且不要忘记在TeamsNameCell
的构造函数中分配此标签:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
[self addSubview:self.name];
}
return self;
}
或者使用默认单元格的标签可能更好:
而不是cell.name.text = @"text";
使用cell.textLabel.text = @"text";
答案 1 :(得分:0)
解决方案是,
static NSString *CellIdentifier = @"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.labelText.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
NSLog(@"%d teams",[teams count]);
return cell;