可扩展的; NSMutableArray中的可折叠UITableView部分

时间:2013-07-03 23:00:14

标签: objective-c uitableview

嗨我有一个表格,里面有一个带有地址的json的信息,我设法按下按钮按邻居排序,我想创建可扩展的&带有相同按钮的社区的可折叠部分。

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] init];
}
NSString *cellName = [[myArray objectAtIndex:indexPath.row] valueForKeyPath:@"name"];
NSString *cellState = [[myArray objectAtIndex:indexPath.row] valueForKeyPath:@"desc"];
NSString *cellTodo = [NSString stringWithFormat:@"%@: %@", cellState, cellName];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 11.0]];
cell.textLabel.text = cellTodo;
return cell;
}


- (IBAction)PorBarrio:(id)sender {
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"desc" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[myTableView reloadData];
}

2 个答案:

答案 0 :(得分:3)

我的个人投票是SLExpandableTableView。你可以在GitHub找到它。

它非常易于使用,但没有很好的记录;目前没有例子。它基本上是普通UITableView课程的延伸,适用于故事板原型/自定义外观&感觉。因此,如果您已经有UITableView,那么您无需进行大量的重复工作。

我花了一些时间来确定如何实现expandingCellForSection:方法 - 这是我的,这样可以让其他人更轻松一点:

- (UITableViewCell<UIExpandingTableViewCell> *)tableView:(SLExpandableTableView *)tableView expandingCellForSection:(NSInteger)section {

    NSString *CellIdentifier = @"amenitysectioncell_immediate";
    AmenitySectionTableViewCell *cell = (AmenitySectionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[AmenitySectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdientifier];
    }

    cell.sectionTitle.text = @"blah blah";

    return cell;
}

诀窍是为你的section单元子继承UITableViewCell

@interface AmenitySectionTableViewCell : UITableViewCell<UIExpandingTableViewCell>  

@property (nonatomic,strong) IBOutlet UILabel *sectionTitle; 

@end

享受!

答案 1 :(得分:0)

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

*使用uiTableView委托方法&#34; viewForHeaderInSection&#34;并返回自定义UIView。

*将UIButton添加为带有操作的子视图&#34; expandable:(id)sender&#34;将发件人ID检查为部分编号并重新加载表视图。

检查此链接:

http://iostechnotips.blogspot.in/2014/05/expandable-uitableview.html

示例:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection(NSInteger)section {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,2 , tableView.frame.size.width, 20)];

    UIButton * headerButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [headerButton addTarget:self action:@selector(expandeble:) forControlEvents:UIControlEventTouchDown];
    [headerButton setTag:section];
    [headerButton setTitle:[NSString stringWithFormat:@"Section %d",section] forState:UIControlStateNormal] ;
    [view addSubview:headerButton];
    view.backgroundColor=[UIColor grayColor];
    return view;
}

-(IBAction)expandeble:(id)sender{
    UIButton* myButton = (UIButton*)sender;
    [self exapand:(int)myButton.tag];
}

-(void)exapand:(int)tag{
    if (senctionIndex != tag) {
    senctionIndex=tag;
    [_expandableTable reloadData];
}}