0

uicollectionview のサブビューとしてプログラムで作成された uitableview を生成する際に問題があります。

ここに私のコードがあります:

.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 に答える