ios - 如何在 ios 中逐行设计自定义单元格?

标签 ios objective-c uitableview customization custom-cell

谁能帮帮我?过去一周我一直致力于扩展单元格,最后我可以在其中添加子菜单。我设计了两个自定义单元格,并使用 plist 添加了菜单和子菜单。它运行良好,我添加了菜单和子菜单。现在我的问题是我只想使用我分配的 indexpath.row 将图像和按钮添加到第 1、2、4、6 行,但是这段代码将图像和按钮分配给所有行但我只想添加到第 1、2、4 行,6 只有我能做到这一点请有人帮助我???

interface MyHomeView ()
{

    NSMutableArray *LeftPaneList;
    NSArray *datalist;

}
@property (assign)BOOL isOpen;
@property (nonatomic,retain)NSIndexPath *selectIndex;
@end

  - (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *path  = [[NSBundle mainBundle] pathForResource:@"LeftPaneMenuList" ofType:@"plist"];
    LeftPaneList = [[NSMutableArray alloc] initWithContentsOfFile:path];



    self.isOpen=NO;

 }


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [LeftPaneList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isOpen) {
        if (self.selectIndex.section == section) {
            return [[[LeftPaneList objectAtIndex:section] objectForKey:@"SubMenu"] count]+1;;
        }
    }
    return 1;
}


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




    if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {

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

        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        NSArray *list = [[LeftPaneList objectAtIndex:self.selectIndex.section] objectForKey:@"SubMenu"];
        cell.name.text = [list objectAtIndex:indexPath.row-1];



        return cell;


   }

    else
    {
        static NSString *CellIdentifier = @"MyHomeViewCell";
       MyHomeViewCell *cell = (MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        cell.tag=indexPath.row;


        NSString *name = [[LeftPaneList objectAtIndex:indexPath.section] objectForKey:@"MenuName"];

cell.MyHomeMenuLabel.text = name;
        return cell;




}
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([indexPath isEqual:self.selectIndex]) {
            self.isOpen = NO;
            [self didSelectCellRowFirstDo:NO nextDo:NO];
            self.selectIndex = nil;

        }else
        {
            if (!self.selectIndex) {
                self.selectIndex = indexPath;
                [self didSelectCellRowFirstDo:YES nextDo:NO];

            }else
            {

                [self didSelectCellRowFirstDo:NO nextDo:YES];
            }
        }

    }


    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
    self.isOpen = firstDoInsert;


    [self.MyHomeTableView beginUpdates];

    int section = self.selectIndex.section;
    int contentCount = [[[LeftPaneList objectAtIndex:section] objectForKey:@"SubMenu"] count];
    NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
    for (NSUInteger i = 1; i < contentCount + 1; i++) {
        NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];




        [rowToInsert addObject:indexPathToInsert];
    }

    if (firstDoInsert)
    {   [self.MyHomeTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
    }
    else
    {
        [self.MyHomeTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
    }



    [self.MyHomeTableView endUpdates];
    if (nextDoInsert) {
        self.isOpen = YES;
        self.selectIndex = [self.MyHomeTableView indexPathForSelectedRow];
        [self didSelectCellRowFirstDo:YES nextDo:NO];
    }
    if (self.isOpen) [self.MyHomeTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}

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

这是原来的 o/p! enter image description here

但我想要这样的o/p enter image description here

有人帮帮我吗??

最佳答案

您需要指定 IndexPath.section 首先,您可以使用 IndexPath.row 进行检查,例如以下示例:-

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

    if(indexPath.section==0)
    {
        if(indexPath.row==0)
        {

           cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];
        }
        else if(indexPath.row==2)
        {
          cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];

        }
        else if(indexPath.row==2)
        {
          cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];

        }
        else if(indexPath.row==4)
        {
          cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];

        }
        else if(indexPath.row==6)
        {

          cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];
        }
        else
       {
         cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
       }
    }

}

关于ios - 如何在 ios 中逐行设计自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275469/

相关文章:

iphone - 如何重新创建分发配置文件

ios - 为什么使用 UIImageJPEGRepresentation 方法通过 writetofile 保存的 .jpeg 文件比 ios 中的 UIImageWriteToSavedPhotosAlbum 大

ios - uitableview更新错误

ios - Swift:如何从 TableView 中删除重复项?

ios - 嵌套的 UIStackview 问题,删除它们会导致崩溃。如何正确去除它们?

iphone - 第二 View 中的标签栏

iphone - 已识别 UITavleViewCell 标签中的点击手势,但也不选择单元格

ios - 使用 Facebook 登录时从未调用过 PFLoginViewController didLogin

ios - 如何防止第二次点击 UITableView

objective-c - 调用viewDidLoad()中的方法。使用索引路径