0

誰でも私を助けることができますか?過去 1 週間、セルの拡張に取り組んでいます。ようやくサブメニューを追加できるようになりました。2 つのカスタム セルを設計し、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

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 に答える