简体   繁体   English

如何在一个表视图中创建两个自定义单元格

[英]How to create two custom cell in one table view

I have a tableview on that I want to create two custom cells. 我要创建两个自定义单元格上有一个tableview I have created two custom classes for UITableViewcell . 我为UITableViewcell创建了两个自定义类。 If I am creating one class cell at a time. 如果我一次创建一个班级单元。 Then it is working right. 然后它工作正常。 But if I am creating both at same time. 但是,如果我同时创建两者。 It is showing exceptions. 它显示了异常。

Code: 码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    check=@"a";


    // Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillLayoutSubviews
{
    tb=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    tb.delegate=self;
    tb.dataSource=self;
    [self.view addSubview:tb];

    [tb registerClass:[imgTableViewCell class] forCellReuseIdentifier:CI];

    [tb registerClass:[_TableViewCell class] forCellReuseIdentifier:CII];  // I think, problem is here.


}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    if([check isEqualToString:@"a"])
    {

    IMGCELL=[tableView dequeueReusableCellWithIdentifier:CI];

    if(!IMGCELL)
    {

    IMGCELL=[[imgTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
    }
    IMGCELL.feeds.text=@"HEHEHE";

    return IMGCELL;

    }

    else
    if([check isEqualToString:@"b"])
    {

    CELL=[tableView dequeueReusableCellWithIdentifier:CII];

    if(!CELL)
    {

    CELL=[[_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CII];
    }
    CELL.IMGV.image=[UIImage imageNamed:@"band"];

    return CELL;
    }
    return nil;

}

Exceptions: 例外:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TableViewCell feeds]: unrecognized selector sent to instance 0x8d5c660'

Problem:Problem is, I am registering two classes to table view. 问题:问题是,我正在向表视图注册两个类。 Is there any method to unregister the class. 是否有任何方法可以unregister该类。

As usual, Define the following datasource methods of the tableview for loading the tableview with some data. 与往常一样,定义表视图的以下数据源方法以将某些数据加载到表视图中。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

in cellForRowAtIndexPath Method 在cellForRowAtIndexPath方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                              reuseIdentifier:CellIdentifier];
        }

        containerObject = [objectCollection objectAtIndex:indexPath.row];
        [[cell textLabel] setText:containerObject.store];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    } else {
        static NSString* cellIdentifier1 = @"FeatureCell";

        FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell1 == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];
            cell1 = (FeatureCell*)[nib objectAtIndex:0];
        }

        cell1.img1.image = [UIImage imageNamed:@"shower.png"];
        cell1.img2.image = [UIImage imageNamed:@"parking.png"];

        return cell1;
    }
}

OR Another SOlution 或另一种解决方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    //We are doing some rough calculation for loading different xibs or different rows 
    if (indexPath.row % 2 == 0) {
        BlogpostCell *cell = (BlogpostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"BlogpostCell" owner:self options:nil];
//Assign the instance of the tableview cell that we have created in the custom cell
            cell = blogPostCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.postType.text = @"Cook said Apple has acquired 23 companies in the last 16 months and remained on the lookout for interesting technology and companies.Apple is not in a race to acquire the most companies or to spend the most money, but that doesn't mean we won't buy a huge company tomorrow afternoon, he said.And he warned shareholders not to focus too narrowly on short-term gains.If you're in Apple for only a week ... or two months, I would encourage you not to invest in Apple,he said.We are here for the long term";

        return cell;
    }
    else if (indexPath.row % 3 == 0) {
        MicroblogCell *cell = (MicroblogCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"MicroblogCell" owner:self options:nil];

            cell = microBlogCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.postContent.text = @"After trying to penetrate the mid-range mobile segment with the outdated iPhone 4, Apple India has decided to offer buyback discounts on the MacBook if you are returning your old Apple laptop";

        return cell;
    }
    else {
        ShareCell *cell = (ShareCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"ShareCell" owner:self options:nil];

            cell = shareCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.postContent.text = @"After smartphones, now almost every tech company is jumping onto smart wearable devices, many of them focusing on health, nutrition and fitness.";
        return cell;
    }
}

In the above code , where we are looking for a specific row index for loading the corresponding cell and returning the same. 在上面的代码中,我们在其中寻找特定的行索引以加载相应的单元格并返回它。 We can also further customise the cell by calculating the dynamic height for different contents and showing the same 我们还可以通过计算不同内容的动态高度并显示相同内容来进一步自定义单元格

I think this is the bast way to create custom cells to tableView, my example include delegate for button action inside a cell, try to use it, and i hope you resolve your problem, and this realization is useful to you in future projects. 我认为这是为tableView创建自定义单元格的最佳方式,我的示例包括在单元格内执行按钮操作的委托,并尝试使用它,希望您解决问题,并且这种实现对将来的项目很有用。 For example your table view delegate method should look like here> 例如,您的表视图委托方法应如下所示>

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     if (indexPath.row == 0)
     {
         CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
         if(!cell){
             cell = (CustomTableViewCell *)[CustomTableViewCell cell];
         }
         cell.tapDelegate = self;
         [cell fillCellWith:indexPath andSomeThingElse:@"test"];

         return cell;

     }
     else
     {
         CistomTableViewCell2 * cell = [tableView dequeueReusableCellWithIdentifier:@"CistomTableViewCell2"];
         if(!cell){
         cell = (CistomTableViewCell2 *)[CistomTableViewCell2 cell];
     }

         return cell;

}

If you have a questions, about customizing this realization, i can help you... 如果您有关于自定义此实现的问题,我可以为您提供帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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