Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在tableView中添加5个不同的自定义单元格_Iphone_Ios_Ios5_Uitableview - Fatal编程技术网

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

Iphone 如何在tableView中添加5个不同的自定义单元格,iphone,ios,ios5,uitableview,Iphone,Ios,Ios5,Uitableview,我想在我的tableview中显示来自web的不同数据,但我不知道如何在表的一个部分的单独单元格中显示它们,有人能帮我显示吗 在一间牢房里 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.te

我想在我的tableview中显示来自web的不同数据,但我不知道如何在表的一个部分的单独单元格中显示它们,有人能帮我显示吗 在一间牢房里

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;
}
请尝试以下代码:

- (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;
}

希望这个答案会有所帮助。干杯

IKQ的代码示例应该可以使用。但我建议试试我的图书馆。这样,代码将更加清晰和优雅:

- (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

我通过编写此代码解决了此问题,感谢大家的建议

- (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;
}

您可以简而言之使用此代码

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

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

我想如果你接受更多的答案,你会得到更多的帮助。这就是我在SO上的经验。另外,你是在问如何创建5个不同的自定义单元格,还是仅仅用“stock iOS”更改数据UITableViewCells?我只是想通过创建一个自定义单元格来节省时间,但是如果你想用另外5个自定义单元格来显示它,那么okI只是想更好地了解你想做什么;下面是答案!我是唯一一个发现为每个单元格创建
UIViewController
很奇怪的人吗?我已经试过了,它不起作用,只给我第一个单元格:(你能再试一次吗?我没有使用同一个对象将值传递给textLabel。那么,你确定你的对象属性是NSString类型吗?我相信提问者试图将数组中每个元素的所有数据都放到一个单元格中…我想这只给他第一个单元格的原因是因为他的数组中只有一个元素,而当e app尝试填充表格,但只会给他第一个单元格。一定要创建一个自定义单元格,以容纳从数组“dataArray”提取的“application”对象中的所有数据。请查看此链接Hi El Guapo,如果询问者的意思是像你的评论一样,应该跟踪你的建议。最佳:)
            CellForInfo * cellForInfo = (CellForInfo *)[tableView dequeueReusableCellWithIdentifier:nil];

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