多个自定义单元格

Multiple Custom Cells

我正在尝试将名为 "ProfileCell1" 的自定义 .XIB 单元格放入我的 table 视图(大小 150)的第一个单元格中,然后将我的第二个自定义 .XIB 单元格设为 "ProfileCell2" 进入第二个单元格(大小 60)。

我可以将 "ProfileCell1" 放入第一行,但我不知道如何将第二行放入,因为当我将 return 值从 1 增加时,第一行会重复在 "numberOfRowsInSection".

到目前为止,这是我的代码:

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier = @"ProfileCell1";
     ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

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

     cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
     return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      return 150;
}

所以我知道这段代码重复了我的 ProfileCell1,但我就是不知道如何让它只出现一次,然后从第二个单元格开始我的 ProfileCell2,并让两个单元格 return 2 'heightForRowAtIndexPath.

中的不同值

我尝试过类似的东西:

if (indexPath.row == 0) {
    return 150;
else {
    return 60;
}

不过好像不行。干杯!

试试这个方法

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
    return 1;// your require number of cell
}
else
{
   return 1;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        static NSString *identifier = @"ProfileCell1";
        ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

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

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    else
    {
        // your second cell coding here
        static NSString *identifier = @"ProfileCell2";
        ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell2" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        // your second cell code 
        //cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    }

并增加单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 150;
    }
    else
    {
         return 60;
    }
}

您可以使用两个部分在同一 UITableView 中使用两个不同的单元格。第一部分使用第一个单元格 .xib,第二部分使用第二个 .xib 文件。

如何识别要放置 ProfileCell1ProfileCell2[= 的 indexPath 11=]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     // say you want to use ProfileCell1 in first row
     if(indexPath.row == 0) {
         static NSString *identifier = @"ProfileCell1";
         ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

         // cell properties

         return cell;
     } else {
         static NSString *identifier = @"ProfileCell2";
         ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];

         // cell properties

         return cell;
     } 
}

您也可以在单个 section 中使用两个不同的 cells。对 Dharmesh Dhorajiya 的 Post 进行一些编辑。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        static NSString *identifier = @"ProfileCell1";
        ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

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

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    else
    {
        // your second cell coding here
        static NSString *identifier = @"ProfileCell2";
        ProfileCell2 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];

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

        cell.profilePictureView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        return cell;
    }
    }

并使用与 return 两个不同单元格高度相同的代码。

if (indexPath.row == 0) {
    return 150;
else {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.row == 0) 
    return 150;
  else 
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   if (indexPath.row == 0) 
    {
      static NSString *identifier = @"ProfileCell1";
      ProfileCell1 *cell = (ProfileCell1 *)[tableView dequeueReusableCellWithIdentifier:identifier];
      //bla bla bla code for ProfileCell1 here
    return cell;
   }
   else
   {
      static NSString *identifier = @"ProfileCell2";
      ProfileCell2 *cell = (ProfileCell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
      //bla bla bla code for ProfileCell2 here
    return cell;
   }

}