2012-03-18 7 views
2

私はビューの上部にツールバーが必要なので、私はtableViewControllerを使用することはできませんので、UIViewController内にtableViewを配置したいと思います。私はtableViewクラスを作成し、それの中に必要な関数があると思ったものを入れましたが、何かが欠落しているか、どこかに間違ったものを置いているに違いありません。あなたはおそらくUITableViewをサブクラス化する必要はありません.HTableView内のUIViewController

import <UIKit/UIKit.h> 

@interface TestTV : UITableView 
@property(strong,nonatomic)NSArray *arr; 

@end 

.M

#import "TestTV.h" 

@implementation TestTV 
@synthesize arr = _arr; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    _arr = [[NSArray alloc]initWithObjects:@"HEJ",@"FOO", nil]; 
    return _arr.count; 
} 

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

    NSString *cellValue = [_arr objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 
} 
@end 

答えて

6

。代わりに、あなたのビューコントローラのサブクラスでは、適切なデリゲートとデータソースのプロトコルを実装するために、あなたの意思を宣言します。

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

次に、あなたのビューコントローラの実装ファイルで、あなたは上記定義したメソッドを実装します。

最後にInterface Builder(またはプログラムで)では、テーブルビューのdelegatedataSourceコンセントの両方をスーパービューのビューコントローラ(IBではこのビューコントローラはファイルの所有者)と等しく設定します。

データアレイをビューコントローラのプロパティにすることもできます。

+0

それがうまくいった!ありがとうございました:) –

+0

テーブルビューのセルやツールバーのボタンからセルを切り離すことはできません –

+0

ストーリーボードのセグを話していると、私はそれに慣れていません。一方、必ず 'IBAction'と' tableView:didSelectRowAtIndexPath: 'デリゲートメソッドを使用して、必要なものを実現してください。 – warrenm

関連する問題