简体   繁体   中英

UITableView not showing when adding it from a custom class

I have a custom class " customClass " that is a subclass UITextField . Here is the setup:

- (void)initWithCoder:(NSCoder *)aDoder {
    [super initWithCoder:aDecoder];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 500) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

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

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    [cell.textLabel setText:@"hey"];
    return cell;
}

I have a textField in the storyboard , so I set its' class to the customClass .

When I run the app, nothing happened. So in setupView , I added the following:

[self.superview insertSubview:self.self.tableView
                     belowSubview:self];

I still didn't see the tableView , so I did this:

[self addSubview:self.tableView];

And the tableView was inside the textField , so I was only able to see part of it.

How can I add the tableView , so it will display normally?

Your code appears to be a implemented to suit a UITableViewController and not a UITextField .

What you'll need to do is create a UITableViewController by dragging one onto the storyboard, and then setting its class to your customClass .

An implementation for UITextField would be much different, and not what you appear be wanting to achieve.

Good Luck!

Programmatic way:

#import "AppDelegate.h"
@interface AppDelegate () <UITableViewDataSource, UITableViewDelegate>
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    UIViewController *controller = [UIViewController new];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 100, 500) style:UITableViewStylePlain];

    tableView.delegate = self;
    tableView.dataSource = self;
    [controller.view addSubview:tableView];
    _window.rootViewController = controller;
    [_window makeKeyAndVisible];

    return YES;
}

// Your Custom Stuff in a separate class:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

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

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    [cell.textLabel setText:@"hey"];
    return cell;
}
@end

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM