0

任何人都可以帮助我吗?过去一周我正在努力扩展单元格最后我可以在其中添加子菜单。我设计了两个自定义单元格并使用 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!在此处输入图像描述

但我想要这样的o/p在此处输入图像描述

谁来帮帮我??

4

3 回答 3

2

您需要先指定,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];
       }
    }

}
于 2013-08-17T05:25:38.367 回答
1

一种简单的方法是在情节提要中设置 2 个不同的动态原型单元,每个单元都有自己的标识符。在 cellForRowAtIndexPath 中:根据 indexPath 将正确类型的单元格出列。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (indexPath.row = 1 || indexPath.row = 2 || indexPath.row = 4 || indexPath.row = 6){
        cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    }
于 2013-08-16T15:05:57.273 回答
0

当您使用dequeueReusableCellWithIdentifier:方法时,它只会将索引路径 2、4、6 等处的单元格提供给 tableView 中的其他单元格。而是使用数组来存储您的单元格,然后从数组中检索它。然后你可以使用 indexpath.row

于 2013-08-16T14:26:56.630 回答