0

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

但是有点失落。

这是它的样子:

在此处输入图像描述

这是我在界面生成器中的设置:

在此处输入图像描述

这是我的代码:

头文件:

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

2 回答 2

1

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

于 2012-08-12T21:37:30.340 回答
0

在界面生成器中,单击您制作的自定义单元格,记住高度;然后单击您要使用的表格视图,从您的记忆中输入行高。

于 2013-09-10T14:05:58.270 回答