0

我在填充以编程方式创建的 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;
}

我的表格视图显示正确,这当然要归功于此处未提及的其他功能,但子视图中没有行。编译器打印出团队的数量,因此它调用委托方法,但它不填充子视图......

这有什么问题?

在此先感谢达里奥

4

2 回答 2

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

于 2012-10-24T15:17:49.513 回答
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;
于 2012-10-24T15:35:15.370 回答