如何显示已在界面构建器中定义的自定义单元格?

时间:2012-08-12 20:25:22

标签: ios uitableview

我正在尝试嵌入自定义设计的TableView Cells(在界面构建器中)。对于初学者,我试图在自己的部分添加一些文本字段。我能够显示自定义设计的tableview但是我做的事情不正确,因为它看起来都搞砸了。关于如何添加其他部分也不是很清楚。有人可以指导我正确的方向吗?我试过以下:http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20

但有点失落。

这是它的样子:

enter image description here

这是我在界面构建器中的设置:

enter image description here

这是我的代码:

头文件:

#import <UIKit/UIKit.h>

@interface SearchTableViewController : UITableViewController
{

}
@property (nonatomic, retain) IBOutlet UITableViewCell *priceRanceCell;
@end

实现:

#import "SearchTableViewController.h"


@interface SearchTableViewController ()

@end

@implementation SearchTableViewController
@synthesize priceRanceCell=_priceRanceCell;




- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Some Search", @"Some Search");
        self.tabBarItem.image = [UIImage imageNamed:@"search"];
    }
    return self;
}




- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1;
}

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

    // Configure the cell...
    NSInteger section = [indexPath section];


    switch (section) {
        case 0: // First cell in section 1


            return self.priceRanceCell;


            break;
        default:
            // Do something else here if a cell other than 1,2,3 or 4 is requested

            return cell;

            break;
    }








    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

2 个答案:

答案 0 :(得分:1)

您需要实施heightForRowAtIndexPath:。另请注意,当您为dequeueReusableCellWithIdentifier:时,结果可能为零,在这种情况下,您需要创建一个单元格。你应该修改你的逻辑,这样你甚至不会尝试它的第0部分。

答案 1 :(得分:0)

在界面构建器中,单击您创建的自定义单元格,记住高度;然后单击要使用的表格视图,从内存中输入行高。