【问题标题】:How to design custom cell row wise individually in ios?如何在ios中单独设计自定义单元格行?
【发布时间】:2013-08-16 14:17:37
【问题描述】:

谁能帮帮我?过去一周我正在努力扩展单元格最后我可以在其中添加子菜单。我设计了两个自定义单元格并使用 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

有人帮我吗??

【问题讨论】:

    标签: ios objective-c uitableview customization custom-cell


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这样做的简单方法是在情节提要中设置 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];
          }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我将单元格存储在数组中并使用了 indexpath.row 但它不起作用
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        相关资源
        最近更新 更多