13

私はiOSアプリケーションとObjectiveC自体の開発に慣れていないので、おそらく非常に簡単な質問があります。

現在、ツールバーボタンのクリックから呼び出される次のメソッドがあります。このメソッドは、フレーム変数frにテーブルビューを作成するように設計されています。

- (IBAction)addGolfer:(id)sender {
    CGRect fr = CGRectMake(101, 45, 100, 416);

    UITableView *tabrleView = [[UITableView alloc]
      initWithFrame:fr
      style:UITableViewStylePlain];

    tabrleView.autoresizingMask =
      UIViewAutoresizingFlexibleHeight |
      UIViewAutoresizingFlexibleWidth;
    tabrleView.delegate = self;
    tabrleView.dataSource = self;
    [tabrleView reloadData];

    self.view = tableView;
}

このメソッドを呼び出した結果は、私が期待するものではありません。フレーム「fr」にテーブルビューを作成する代わりに、テーブルビューが画面全体に表示されます。

繰り返しになりますが、私はまったく新しいので、回答や提案をいただければ幸いです。ありがとう!

4

3 に答える 3

20

dataSourcedelegateプロパティを設定するときUITableViewは、少なくとも次のメソッドを作成する必要があることを意味しますdataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

これを行わないと、クラッシュします。要約すると、これがわかります(このコードには、構文エラーまたは論理エラーが含まれている可能性があります。メモ帳で記述しました)。

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *firstTableView;
    UITableView *secondTableView;
}

@end

//

@implementation YourViewController

#pragma mark - Objects Processing

- (void)addGolfer:(UIBarButtonItem *)sender {
    if (secondTableView) {
        [secondTableView removeFromSuperView];
        secondTableView = nil;
    }

    secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)];
    secondTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    secondTableView.delegate = self;
    tabrleView.dataSource = self;

    [self.view addSubview:secondTableView];
}

#pragma mark - TableView DataSource Implementation

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == firstTableView) { // your tableView you had before
        return 20; // or other number, that you want
    }
    else if (tableView == secondTableView) {
        return 15; // or other number, that you want
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    cell.backgroundView = [[UIView alloc] init];
    [cell.backgroundView setBackgroundColor:[UIColor clearColor]];
    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    if (tableView == firstTableView) { // your tableView you had before
        // ...
    }
    else if (tableView == secondTableView) {
        cell.titleLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row + 1];
    }

    return cell;
}

@end
于 2012-04-04T06:08:10.370 に答える
17

ステップ1:デリゲートを追加するUITableViewDataSource,UITableViewDelegate

@interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate>
{
   UITableView *tableView;
}

ステップ2:

-(void)viewDidLoad
{
    tableView=[[UITableView alloc]init];
    tableView.frame = CGRectMake(10,30,320,400);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [tableView reloadData];
    [self.view addSubview:tableView];
}

ステップ3:テーブルビューのプロパティ(行と列)

//-テーブル内の行数がない場合

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//-必要に応じてテーブルヘッダーの高さ

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 50;
}

//-セルにデータを割り当てる

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

   if (cell == nil)
   {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = @"Hello";
   return cell;
}

//-セルに触れるときの操作

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Your custom operation
}
于 2014-02-04T05:13:07.410 に答える
4

のビューを設定する代わりにUIViewController、tableViewをサブビューとして追加します。

それ以外の:

self.view = tableView;

これを行う:

[self.view addSubview:tableView];

これにより、設定したフレームが適切に尊重されます。

于 2012-04-03T23:43:54.697 に答える