Iphone 正在为单元格进行自定义,但不起作用

Iphone 正在为单元格进行自定义,但不起作用,iphone,Iphone,我的目的是为我的表视图的一行自定义一个单元格。因此,我正在做: MyClass.m 1 .- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 3 . NSLog(@"section %d:row %d",indexPath.section, indexPath.row); 4 . static NSStr

我的目的是为我的表视图的一行自定义一个单元格。因此,我正在做:

MyClass.m
    1 .- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{  
    3 .    NSLog(@"section %d:row %d",indexPath.section, indexPath.row);
    4 .    static NSString *CellIdentifier = @"reusedCell";   
    5 .    DetailCell *cell    = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    6 .    cell.backgroundColor    = [UIColor clearColor];
    7 .    // Customize the cell of each row from table
    8 .    **if ( cell == nil ) {
    9 .        cell    =   [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    10.    }**
    11.    return cell;
    12.}
我的课程是

    1.- (DetailCell*)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    2.   if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]){
    3.  itemImg     =   [[UIImageView alloc]    initWithFrame:CGRectMake(20 , 20, 50  , 70)];
    4   itemName    =   [[UILabel  alloc]       initWithFrame:CGRectMake(100, 0 , 100 , 50)];
    5.  itemDesp    =   [[UILabel  alloc]       initWithFrame:CGRectMake(100, 40, 1000, 20)];
    6   itemSize    =   [[UILabel  alloc]       initWithFrame:CGRectMake(100, 60, 100 , 20)];
    7   itemPrice   =   [[UILabel  alloc]       initWithFrame:CGRectMake(200, 60, 100 , 20)]; }
    8.   [itemImg setImage:[UIImage imageNamed:@"large/dresses/02/71EU6.png"]] ;
    9.   itemName.text = @"Dresses";
    10.   itemDesp.text = @"Description";
    11.   itemSize.text = @"S";
    12.   itemPrice.text = @"$99";
    13.   itemSize.textColor = [UIColor purpleColor];

    14.   [self.contentView addSubview:itemImg];
    15.   [self.contentView addSubview:itemName];
    16.   [self.contentView addSubview:itemDesp];
    17.   [self.contentView addSubview:itemSize];
    18.   [self.contentView addSubview:itemPrice];
    19.        return self;
    20.    }
要在我的表格的行上显示img或文本,我在UIImageView上设置img+文本,并在DetailCell中设置标签(第8行-->DetailCell的第13行)。 然而,当我阅读一些阅读资料时,我意识到人们使用DetailCell这样的类来为UIImageView+标签创建框架+添加属性。最后,它们将在myClass.m中的单元格上显示图像或文本。 这让我很困惑,因为我们应该将UIImageView的img设置为MyClass.m还是DetailCell.m
关于这个问题,请给我一些建议。谢谢。

如果电池不是空的,它将无法工作。您通常会根据节或行配置单元格。您正在尝试自定义哪些单元格?假设你希望第一个单元格的样式是这样的,而其他单元格不是

switch (indexPath.row) {
    case 0: 
        cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:CellIdentifier];
        break;
    default:
        /*
         * Configure the rest of the cells... 
         *
         * Use case 1, 2, 3, 4, etc. as needed.
         * You can then use default to set the default configuration
         */
         break;
 }
你可以试试那样的方法。另外,请确保您没有:

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

这是默认设置,将限制可以拥有的单元格数量。如果您有多个部分,请使用与上一个示例中使用的类似的switch语句。

是的,我在[link]**code**
code
上的帖子中遇到了一个问题。我不知道为什么我的手机第一次不是零。rite,你的切换方法是cellForRowAtIndexPath吗?为什么我们不应该让numberOfRowsInSection=1。我认为这不是问题,因为您可以有一个包含多行(或单元格)的节。