自定义UITableViewCell打开不同的xib子视图

发布于 2024-10-02 06:56:11 字数 2615 浏览 0 评论 0原文

我已经开始学习 xcode 和 Iphone SDK,我为 tableview 制作了一个自定义 UITableviewCell,仅通过一个 xib(nib) 文件加载不同值的 NSArray。

我想知道的是如何让这些单元格打开不同的 xib(nib) 文件。

这是我的文件。

#import "SimpleTableViewController.h"
#import "NextViewController.h"
#import "TableCellView.h"

@implementation SimpleTableViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
    titleList = [[NSArray alloc] initWithObjects:
        @"Title 1",     @"Title 2",     @"Title 3",     @"Title 4"  , nil]; 
    imagesList = [[NSArray alloc] initWithObjects:
        @"Image 1",     @"Image 2",     @"Image 3",
        @"Image 4",     nil];   
    imagesHeader = [[NSArray alloc] initWithObjects:
        @"ImagePro 1",  @"ImagePro 2",
        @"ImagePro 3",  @"ImagePro 4",  nil];
    descpList = [[NSArray alloc] initWithObjects:   @"Description 1",
        @"Description 2",   @"Description 3",
        @"Description 4",   nil];   

    self.title = @"Text";
    [super viewDidLoad];
}


#pragma mark Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [titleList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";    
    MyIdentifier = @"tblCellView"; 
    TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if(cell == nil) {       
        [[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil]; 
        cell = tblCell;
    }       
    [cell setLabelText:[titleList objectAtIndex:indexPath.row]];
    [cell setProductImage:[imagesList objectAtIndex:indexPath.row]]; 
    [cell setDescpText:[descpList objectAtIndex:indexPath.row]];
    return cell; 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NextViewController *nextController = [[NextViewController alloc] initWithNibName:@"NextView" bundle:nil];
    [self.navigationController pushViewController:nextController animated:YES];
    [nextController changeProductText:[titleList objectAtIndex:indexPath.row]];
    [nextController changeProductContent:[descpList objectAtIndex:indexPath.row]];
    [nextController changeHeaderContent:[imagesHeader objectAtIndex:indexPath.row]]; 
}

@end

I have started to learn xcode and Iphone SDK, I have made a custom UITableviewCell for a tableview that load NSArrays for different values through only one xib(nib) file.

What I want to know is how to make these cells opens different xib(nib) files.

Here is my file.

#import "SimpleTableViewController.h"
#import "NextViewController.h"
#import "TableCellView.h"

@implementation SimpleTableViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
    titleList = [[NSArray alloc] initWithObjects:
        @"Title 1",     @"Title 2",     @"Title 3",     @"Title 4"  , nil]; 
    imagesList = [[NSArray alloc] initWithObjects:
        @"Image 1",     @"Image 2",     @"Image 3",
        @"Image 4",     nil];   
    imagesHeader = [[NSArray alloc] initWithObjects:
        @"ImagePro 1",  @"ImagePro 2",
        @"ImagePro 3",  @"ImagePro 4",  nil];
    descpList = [[NSArray alloc] initWithObjects:   @"Description 1",
        @"Description 2",   @"Description 3",
        @"Description 4",   nil];   

    self.title = @"Text";
    [super viewDidLoad];
}


#pragma mark Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [titleList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";    
    MyIdentifier = @"tblCellView"; 
    TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if(cell == nil) {       
        [[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil]; 
        cell = tblCell;
    }       
    [cell setLabelText:[titleList objectAtIndex:indexPath.row]];
    [cell setProductImage:[imagesList objectAtIndex:indexPath.row]]; 
    [cell setDescpText:[descpList objectAtIndex:indexPath.row]];
    return cell; 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NextViewController *nextController = [[NextViewController alloc] initWithNibName:@"NextView" bundle:nil];
    [self.navigationController pushViewController:nextController animated:YES];
    [nextController changeProductText:[titleList objectAtIndex:indexPath.row]];
    [nextController changeProductContent:[descpList objectAtIndex:indexPath.row]];
    [nextController changeHeaderContent:[imagesHeader objectAtIndex:indexPath.row]]; 
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-10-09 06:56:11

创建 XIB 文件。 (CustomCellOne、CustomCellTwo)

以下是如何交替单元格的示例:

更改这些行: 更改

MyIdentifier = @"tblCellView";

[[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];

为这些行:

MyIdentifier = indexPath.row % 2 == 0 ? @"CustomCellOne" : @"CustomCellTwo";

[[NSBundle mainBundle] loadNibNamed:indexPath.row % 2 == 0 ? @"CustomCellOne" : @"CustomCellTwo" owner:self options:nil];

Create the XIB files. (CustomCellOne, CustomCellTwo)

Here's an example of how to alternate the cells:

Change these lines:

MyIdentifier = @"tblCellView";

[[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];

To these lines:

MyIdentifier = indexPath.row % 2 == 0 ? @"CustomCellOne" : @"CustomCellTwo";

[[NSBundle mainBundle] loadNibNamed:indexPath.row % 2 == 0 ? @"CustomCellOne" : @"CustomCellTwo" owner:self options:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文