如何在tableView中添加5个不同的自定义单元格

7
我想在我的表格视图中展示不同的网络数据,但我不知道如何在一个部分的表格中显示它们。有人能帮助我将它们显示在一个单元格中吗?
cell.textLabel.text=app.i_name;

在第二个单元格中。
cell.textLabel.text=app.i_phone;

第三个单元格
cell.textLabel.text=app.i_hours;

在第四个单元格中
cell.textLabel.text=app.i_address;

第五个单元格
cell.textLabel.text=app.i_email;

我在索引处的单元格如下

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

    static NSString *CellIdentifier = @"Single Cell";
    SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        UIViewController *c = [[UIViewController alloc] initWithNibName:@"SingleCell" bundle:nil];
        cell = (SingleCell *) c.view;
        //[c release];

    }
appDC * application = [dataArray objectAtIndex:[indexPath row]];

        //cell.namelbl.text=application.application_name;


return cell;
}

2
在我看来,如果你接受更多的答案,你会得到更多的帮助。这是我在 Stack Overflow 上的经验。 - El Guapo
1
另外,您是在询问如何创建5个不同的自定义单元格,还是只是更改“原生iOS” UITableViewCells中的数据? - El Guapo
我只是想更好地了解你想做什么;下面的答案是正确的选择! - El Guapo
我已经编辑了我的答案。让我们再试一次。干杯! - IKQ
1
我是唯一一个觉得为每个单元格创建一个 UIViewController 很奇怪的人吗? - sooper
显示剩余2条评论
5个回答

5
尝试这个代码:

试一下这段代码:

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

    static NSString *CellIdentifier = @"Single Cell";
    SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        UIViewController *c = [[UIViewController alloc] initWithNibName:@"SingleCell" bundle:nil];
        cell = (SingleCell *) c.view;
        //[c release];

    }
    appDC * application = [dataArray objectAtIndex:[indexPath row]];

    //cell.namelbl.text=application.application_name;
    if (indexPath.row == 0)
    {
        cell.textLabel.text=application.i_name;
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = application.i_iphone;
    }
    else if (indexPath.row == 2)
    {
        cell.textLabel.text = application.i_hours;
    }
    else if (indexPath.row == 3)
    {
        cell.textLabel.text = application.i_address;
    }
    else
    {
        cell.textLabel.text = application.i_email;
    }



    return cell;
}

希望我的回答能够帮到您。祝好!

我已经尝试过了,它不起作用,它只给我第一个单元格 :( - Noor
你能再试一次吗?我没有使用相同的对象将值传递给textLabel。所以,你确定你的对象属性是NSString类型吗? - IKQ
3
我认为提问者想要将数组中每个元素的所有数据放入一个单元格中...我猜测之所以只给他第一个单元格是因为他的数组只有一个元素,当应用程序尝试填充表格时,只给了他第一个单元格。需要创建自定义单元格来容纳从数组"dataArray"中提取的"application"对象中的所有数据。请查看此链接https://dev59.com/f2445IYBdhLWcg3wUoxe - El Guapo
嗨,El Guapo,如果提问者的意思是像您的评论,应该跟踪您的建议方式。最好:) - IKQ

2
我通过编写以下代码解决了这个问题,感谢所有提供建议的人。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"CellForInfo";
    CellForInfo *cell =(CellForInfo *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        UIViewController *c = [[UIViewController alloc] initWithNibName:@"CellForInfo" bundle:nil];
        cell = (CellForInfo *) c.view;
        //[c release];



     }

    appDC * application = [dataArray objectAtIndex:0];
    if (indexPath.row == 0)
    {
        cell.lblinfo.text=application.i_name;
    }
     if (indexPath.row == 1)
    {
        cell.lblinfo.text = application.i_phone;
    }
     if (indexPath.row == 2)
    {
        cell.lblinfo.text = application.i_hours;
    }
     if (indexPath.row == 3)
    {
        cell.lblinfo.text = application.i_address;
    }
    if (indexPath.row == 4)
    {
        cell.lblinfo.text = application.i_email;
    }



    return cell;
}

1

IKQ的代码示例应该可以工作。但我建议尝试我的TableKit库。这样代码会更清晰、优雅:

- (void)viewDidLoad
{
    [super viewDidLoad];

    TKStaticCell* nameCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Name" detailText:app.i_name];
    TKStaticCell* phoneCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Phone" detailText:app.i_phone];
    TKStaticCell* hoursCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Hours" detailText:app.i_hours];
    TKStaticCell* addressCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Address" detailText:app.i_address];
    TKStaticCell* emailCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"email" detailText:app.i_email];
    TKSection* section = [TKSection sectionWithCells:nameCell, phoneCell, hoursCell, addressCell, emailCell, nil];
    self.sections = [NSArray arrayWithObject:section];
}

此外,该库还允许定义自定义单元格而不是TKStaticCell


0

你可以使用这段代码进行简短的操作

            CellForInfo * cellForInfo = (CellForInfo *)[tableView dequeueReusableCellWithIdentifier:nil];

            if (cellForInfo ==nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellForInfo" owner:self options:nil];
                cellForInfo = [nib objectAtIndex:0];
            }

-1
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self tableInfo];
}


- (void) tableInfo
{

    NSArray *keysArray = @[@"CampaignsName",@"BusinessName",@"BusinessType",@"CampaignsType",@"Distance",@"Daysleft",@"CampaignImage",];

    NSArray *valuesArray1 =[NSArray arrayWithObjects:@"5 % OFF ",@"Coffe Shop",@"1",@"1",@"0.10 Miles",@"5 days Left",@"b2.png", nil];

    NSArray *valuesArray2 = [NSArray arrayWithObjects:@"win $2 for you & charity ",@"Red Tommato",@"2",@"2",@"20 Miles",@"2 days Left",@"b1.png", nil];

    NSArray *valuesArray3 = [NSArray arrayWithObjects:@"Buy dogs food & get a one more",@"Pet Care",@"3",@"3", @"30 Miles",@"10 days Left",@"b2.png", nil];

    NSArray *valuesArray4 =[NSArray arrayWithObjects:@"win $2 for you & charity ",@"Red Tommato",@"1",@"1",@"0.10 Miles",@"7 days Left",@"b2.png", nil];



    NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:valuesArray1 forKeys:keysArray];

    NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:valuesArray2 forKeys:keysArray];

    NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:valuesArray3 forKeys:keysArray];

    NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:valuesArray4 forKeys:keysArray];



    self.tableArray = [[NSArray alloc]initWithObjects:dict1,dict2,dict3,dict4,dict3,dict1,dict4,dict2,nil];
    NSLog(@"Array %@",tableArray);
    [self.tableObj reloadData];
}

#pragma  mark table view datasource

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

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}

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

    UITableViewCell *cell = [self tableCustomView:tableView cellForRowAtIndexPath:indexPath];
    return cell;
}

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



    static NSString *CellIdentifier1 = @"CustomCellIdentifier";
    static NSString *CellIdentifier2 = @"CustomCellIdentifier2";


    if (indexPath.row % 2 != 0)
    {
        CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell2 == nil)
        {

            NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell2" owner:self options:nil];
            cell2 = [topObjects objectAtIndex:0];
        }


        id object = [self.tableArray objectAtIndex:indexPath.row];


        cell2.CampaignsNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsName"]];
        cell2.BusinessNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessName"]];
        cell2.DistanceLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Distance"]];
        cell2.DaysleftLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Daysleft"]];

        cell2.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignImage"]]];


        int businessType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessType"]] integerValue];

        NSLog(@": %d",businessType);

        switch (businessType)
        {


            case 1:

                cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"RETL@2x.png"];

                break;
            case 2:

                cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"BTY.png"];

                break;
            case 3:

                cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"];

                break;

            default:
                break;
        }


        int CampaignsType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsType"]] integerValue];

        switch (CampaignsType)
        {

            case 1:

                cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t3.png"];

                break;
            case 2:

                cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t2.png"];

                break;
            case 3:

                cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"];

                break;

            default:
                break;
        }

        mainCell = cell2 ;


    }

    else
    {
        CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell1 == nil)
        {

            NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil];
            cell1 = [topObjects objectAtIndex:0];
        }


        id object = [self.tableArray objectAtIndex:indexPath.row];


        cell1.CampaignsNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsName"]];
        cell1.BusinessNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessName"]];
        cell1.DistanceLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Distance"]];
        cell1.DaysleftLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Daysleft"]];

        cell1.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignImage"]]];


        int businessType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessType"]] integerValue];

        NSLog(@": %d",businessType);

        switch (businessType)
        {


            case 1:

                cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"RETL@2x.png"];

                break;
            case 2:

                cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"BTY.png"];

                break;
            case 3:

                cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"];

                break;

            default:
                break;
        }


        int CampaignsType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsType"]] integerValue];

        switch (CampaignsType)
        {

            case 1:

                cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t3.png"];

                break;
            case 2:

                cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t2.png"];

                break;
            case 3:

                cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"];

                break;

            default:
                break;
        }

        mainCell = cell1 ;
    }


    return mainCell;

    NSLog(@"Index Path is :%d %d", indexPath.section,indexPath.row);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接