2013-08-31 38 views
1
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50; 
} 

在这个地方,我想动态添加一个按钮来增加单元格的高度,所以当用户单击该按钮时,应该增加单元格的高度,然后再次单击以调整高度。如何增加和调整iphone中的tableview单元格?

-(void)IncreaseCell 
{ 
    UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [DownArrow addTarget:self 
        action:@selector(IncreaseCell:) 
     forControlEvents:UIControlEventTouchDown]; 
    //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal]; 
    DownArrow.frame = CGRectMake(121.0, 112.0, 72.0, 37.0); 
    [cell.contentView addSubview:DownArrow]; 

    UIImage *buttonDown = [UIImage imageNamed:@"friendsDownArrowButton.png"]; 
    [DownArrow setBackgroundImage:buttonDown forState:UIControlStateNormal]; 
    [cell.contentView addSubview:DownArrow]; 
} 
+0

是要实现折叠/展开的行选择??? –

+0

是的,请您建议我 –

+0

您是否需要关于此问题的更多帮助?如果其中一个答案解决了您的问题,请接受它。 –

回答

0

所有你需要做的就是声明一个变量:

@interface yourViewController() 
{ 
    CGFloat cellSize; 
} 

而在你heightForRowAtIndexPath:

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

viewDidLoad

我想是这样

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    cellSize = 50; 
} 

,并最后在increaseCell:

-(void)IncreseCell { 
    cellSize += 10; 
    [self reloadData]; 
} 
0

你需要改变你的heightForRowAtIndexPath实现。

添加一些存储,如数组,其中包含指示行是否展开的标志。点击按钮时,获取它所在单元格的索引路径,并编辑数组内容以设置/取消设置标志。重新加载表视图(或更改索引路径的行)。

1
  1. 您将需要创建一个NSMutableArray作为一个实例变量,在你把所有的细胞的轨迹,你希望有“增加”。

    @interface YourTableViewController : UITableViewController { 
        NSMutableDictionary *increasedRows; 
    } 
    

    记得分配/初始化该变量。

  2. 为了让您的细胞增加:

    -(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath { 
        [increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath]; 
    
        [self.tableView beginUpdates]; //Delete if you don't want it animated 
        [self.tableView endUpdates]; //Delete if you don't want it animated 
    
        // [self.tableView reloadData]; //Uncomment if you don't want it animated 
    } 
    
  3. 你改变tableView: heightForRowAtIndexPath:声明来检查这本词典供你indexPath。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
        // Check if cell is increased, return custom height 
        if([[increasedRows objectForKey:indexPath] boolValue]) { 
         return 150; 
        } 
        // Cell isn't increased so return regular height 
        return 50; 
    } 
    

这个方法可以让你做的每一行单独并允许你制作动画。

+0

好啊!理想和优化的解决方案 –

+0

没有如何初始化/分配和我必须做的? –

+0

你将不得不在你的init方法中调用'addsRows = [[NSMutableDictionary alloc] init];''。 –

0

我做了什么在我的项目之一,是我创建了一个自定义单元格添加子视图其静态

1日现场非折叠视图 自定义单元格显示了仅限点击按钮,它会显示通过扩展视图按钮,你应该适当增加使用功能细胞的大小的点击增加细胞的大小..

即..

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue]; 

     if (check) 
     { 
      return 180; ..expanded view height 
     } 
     return 40; collapse view height 
    } 
} 
相关问题