ViewController中的tableView实例

时间:2012-06-25 17:40:41

标签: ios uitableview uiviewcontroller

在上周的洞中,我使用了tableviews。对于像我这样的初学者来说,这是非常困难的事情。我能解决的大多数问题都是我自己解决的。但仍有一件事我没有修复。我的问题:如何让我实现一个工作的tableview作为ViewController中一个实例的子视图? 我发布了我用过的代码。 Table类很好,如果我直接在ViewController中设置它没有问题。但是,如果我想在ViewController中设置为子视图,我可以看到它显示,我可以滚动它,但下拉部分什么都不做。我知道我必须与代表做一些事情,但我不知道到底是什么。

有我的代码:


Table.h

#import <UIKit/UIKit.h>

@interface Table : UITableViewController{
    UITableView *table; 
    NSString *dataForSection0;
    NSString *dataForSection2;
    NSMutableArray *demoData;
    int selectedValueIndex;
    bool isShowingList;
}

@property (retain, nonatomic) NSString *dataForSection0;
@property (retain, nonatomic) NSString *dataForSection2;
@property (retain, nonatomic) NSMutableArray *demoData;
@property (nonatomic) int selectedValueIndex;
@property (nonatomic) bool isShowingList;

@end

Table.m

#import "Table.h"

@interface Table ()

@end

@implementation Table
@synthesize dataForSection0;
@synthesize dataForSection2;
@synthesize demoData;
@synthesize selectedValueIndex;
@synthesize isShowingList;


- (void)viewDidLoad
{
    [super viewDidLoad];

    dataForSection0 = @"This is some cell content.";
    dataForSection2 = @"This is another cell content.";

     demoData = [[NSMutableArray alloc] init];
    [demoData addObject:@"One"];
    [demoData addObject:@"Two"];
    [demoData addObject:@"Three"];
    [demoData addObject:@"Four"];
    [demoData addObject:@"Five"];

    isShowingList = NO;
    selectedValueIndex = 0;

}

- (void)viewDidUnload
{
    [self setDataForSection0:nil];
    [self setDataForSection2:nil];
    [self setDemoData:nil];
    [super viewDidUnload];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"My first section";
    }
    else if (section == 1){
        return @"My demo section";
    }
    else{
        return @"Another section";
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 2) {
        return 1;
    }
    else{
        if (!isShowingList) {
            return 1;
        }
        else{
            return [demoData 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];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.accessoryType = UITableViewCellAccessoryNone;

    [[cell textLabel] setFont:[UIFont fontWithName:@"Marker Felt" size:16.0]];


    if ([indexPath section] == 0) {

        [[cell textLabel] setText:dataForSection0];
    }
    else if ([indexPath section] == 2){

        [[cell textLabel] setText:dataForSection2];
    }
    else{
        if (!isShowingList) {

            [[cell textLabel] setText:[demoData objectAtIndex:selectedValueIndex]];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else{
            [[cell textLabel] setText:[demoData objectAtIndex:[indexPath row]]];

            if ([indexPath row] == selectedValueIndex) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else{
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([indexPath section] == 1) {

        if (isShowingList) {
            selectedValueIndex = [indexPath row];
        }       

        isShowingList = !isShowingList;

        [table reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    }
    else{
        return;
    }

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "Table.h"

@interface ViewController : UIViewController {

    Table *table;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    table = [[Table alloc] initWithStyle:UITableViewStyleGrouped];
    CGRect viewFrame = self.view.frame;
    viewFrame = CGRectMake(50, 50, 300, 800);
    table.view.frame = viewFrame;
    [self.view addSubview:table.view];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

1 个答案:

答案 0 :(得分:0)

在将Tables视图添加为子视图之前,请尝试调用loadView函数。

table = [[Table alloc] initWithStyle:UITableViewStyleGrouped];
CGRect viewFrame = self.view.frame;
viewFrame = CGRectMake(50, 50, 300, 800);
table.view.frame = viewFrame;
[table loadView];
[self.view addSubview:table.view];
相关问题