从NSMutableArray展开和可折叠的UITableView部分
您好我有一个表与信息从一个JSON的地址,我设法按邻居排序他们按下按钮时,我想创建可扩展的&可折叠部分与邻域相同按钮。从NSMutableArray展开和可折叠的UITableView部分
-(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];
}
我的个人投票是针对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;
}
诀窍是继承UITableViewCell
您切片细胞:
@interface AmenitySectionTableViewCell : UITableViewCell<UIExpandingTableViewCell>
@property (nonatomic,strong) IBOutlet UILabel *sectionTitle;
@end
享受!
谢谢@capikaw。请分享您的基本工作示例!如果你有一个UIViewController(和一个UITableView里面),它不工作,但它仍然不工作:(。仍然似乎没有在github上发布可建立的示例。 – tarabyte
@capikaw,它是否与静态单元格一起工作? –
如果你有UIViewController而不是UITableViewController –
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
*使用uiTableView委托方法“viewForHeaderInSection”并返回一个自定义的UIView。
*添加一个UIButton作为子视图的动作“展开:(id)发件人”检查发件人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];
}}
检查苹果示例代码[表视图动画和手势(http://developer.apple.com/library/ios /#samplecode/TableViewUpdates/Introduction/Intro.html)可折叠表格视图 –
检查http://*.com/questions/15442697/how-to-create-an-accordion-with-uitableview-under-a-uitableview – saturngod
我刚刚开始了一个[GitHub项目](ht tps://github.com/i2097i/SRExpandableTableView)用于iOS可展开的UITableView,称为SRExpandableTableView。我希望它有帮助! – i2097i