2012-04-03 38 views
13

Tôi mới phát triển các ứng dụng iOS và Objective C, vì vậy tôi có một câu hỏi rất đơn giản.Tạo một TableVIew Lập trình với Objective-C iOS

Hiện tại tôi có phương pháp sau được gọi từ nút Công cụ nhấp chuột. Phương pháp này được thiết kế để tạo chế độ xem bảng trong biến khung 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; 
} 

Kết quả của việc gọi phương pháp này không phải là những gì tôi mong đợi. Thay vì tạo Bảng Xem trong khung "fr", chế độ xem bảng sẽ lấp đầy toàn bộ màn hình.

Một lần nữa tôi hoàn toàn mới và sẽ đánh giá cao mọi câu trả lời và mọi đề xuất. Cảm ơn!

Trả lời

18

Khi bạn thiết lập dataSourcedelegate thuộc tính cho UITableView của bạn, nó có nghĩa là, bạn phải viết phương pháp này ít nhất cho dataSource:

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

Nếu bạn không làm điều này, nó sẽ sụp đổ.Tóm tắt thông tin bạn sẽ có được điều này (mã này có thể chứa cú pháp hoặc lỗi logic - Tôi đã viết nó bằng notepad):

@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 
4

Thay vì đặt chế độ xem của UIViewController, hãy thêm bảngView làm chế độ xem phụ.

Thay vì:

self.view = tableView; 

Làm điều này:

[self.view addSubview:tableView]; 

này đúng cách sẽ tôn trọng các khung hình mà bạn thiết lập.

+0

tôi đã làm điều đó, nhưng bây giờ không có gì xảy ra. --- Xin lỗi, giờ tôi gặp lỗi. –

+0

Có thể bạn đã sai chính tả 'tabrleView'. Nó sẽ là 'tableView' ở khắp mọi nơi. – DHamrick

+0

Tôi đã làm điều đó bởi vì tôi đã có một TableView khác –

16

Bước 1: Thêm đại biểu UITableViewDataSource,UITableViewDelegate

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

Bước 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]; 
} 

Bước 3: Thuộc tính cho tableview (hàng & cột)

// - Đối với không có hàng trong bảng

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

// - Bảng chiều cao tiêu đề nếu cần thiết

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

// - Chỉ định dữ liệu cho ô

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

// - Operation khi các tế bào cảm ứng

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Your custom operation 
} 
Các vấn đề liên quan