自定义uitableviewcell的问题

C.J*_*hns 0 iphone uitableview uilabel ios

我过去几天一直在玩桌面视图,试图习惯他们,今天我一直在尝试创建一个自定义的uitableviewcell,我正在使用Apple文档中的Table View编程指南,并且遇到了这个奇怪的问题我想做.

我已经设置了一个自定义单元格nib文件,它上面有两个标签,它们都有标签(0和1)我已经将File owner类更改为将使用它的nib.

从那里我开始编写tableView:cellForRowAtIndexPath:方法,在tableview中显示我的自定义单元格,但正如您将看到的,当我为其分配字符串时,只有我的第一个标签显示,我只是不知道为什么...

自定义单元格笔尖 在此输入图像描述

tableView:cellForRowAtIndexPath:方法

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ManufactureSearchCell" owner:self options:nil];
            cell = vehicleSearchCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.vehicleSearchCell = nil;
        }
        // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if(indexPath.section == 0)
        {
            if(indexPath.row == 0)
            {
                UILabel *label;
                label = (UILabel *)[cell viewWithTag:0];
                label.text = [[NSString alloc] initWithString:@"Manufacture :"];

                label = (UILabel *)[cell viewWithTag:1];
                label.text = [[NSString alloc] initWithString:@"empty"];
            }
            else if(indexPath.row == 1)
            {
//.....
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激 :)

Mis*_*Moo 5

请注意使用viewWithTag和0.这是所有视图的默认标记,因此请仔细检查实际上是从中获取标签,而不是单元格的背景视图,附件视图或添加到单元格的任何其他默认视图.它可能会导致问题.

如果您正在重用该单元格,我建议您创建一个自定义UITableViewCell子类并通过IB链接它们.如果你没有重复使用那个单元格,并希望它只显示一次,那么肯定会采用Karoly的建议将这些标签作为IBOutlets.