使用Expandable UITableView仅显示一行

时间:2013-12-10 12:30:26

标签: ios iphone objective-c uitableview

我尝试这样做,如红色正方形所示。

enter image description here

我需要通过单击用户打开一个盘子和DatePicker。为此,我使用Expandable UITableView(http://www.wannabegeek.com/?p=338)。但我无法理解如何在部分中执行此操作可以是一个元素(例如DatePicker)并通过单击该部分显示正好的DatePicker。现在,如果至少有两个元素,那么字符串不会显示为一个节,而只是一个不包含更多元素的字符串。而且好像我没有改变原型单元格的大小不会改变,我也看不到完全的DatePicker。现在我的DatePicker位于单元格的文本后面(这是合乎逻辑的)

两个主要问题:如何更改单元格的大小,查看DatePicker是正常的,以及如何使列表中只有一个项目 在StoryBoard中我看到: enter image description here

在模拟器中: enter image description here

可能更容易做出决定吗?

我使用代码:

    // Exampl2eController.m

    #import "Example2Controller.h"

    @implementation Example2Controller

    @synthesize dataModel = _dataModel;

    - (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

// Custom initialization

}

return self;

}



- (void)viewDidLoad

{

[super viewDidLoad];

_dataModel = [NSMutableArray arrayWithObjects:

[NSMutableArray arrayWithObjects:@"Row 1a", @"Row 2a", @"Row 3a", nil],

[NSMutableArray arrayWithObjects:@"Row 1b", @"Row 2b", nil],

[NSMutableArray arrayWithObjects:@"Row 1c", @"Row 2c", @"Row 3c", @"Row 4c", nil],

[NSMutableArray arrayWithObjects:@"Row 1d", nil],

nil];


}



- (NSInteger)numberOfSectionsInTableView:(ExpandableTableView *)tableView

{

// Return the number of sections.

return [_dataModel count];

}

- (NSInteger)tableView:(ExpandableTableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if ([_dataModel count] == 0) {

return 0;

}

// Return the number of rows in the section.

return [[_dataModel objectAtIndex:section] count];

}

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"RowCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.textLabel.text = [[_dataModel objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

// just change the cells background color to indicate group separation

cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];

return cell;

}

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section

{

static NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.textLabel.text = [NSString stringWithFormat: @"Group %d (%d)", section, [self tableView:tableView numberOfRowsInSection:section]];

// We add a custom accessory view to indicate expanded and colapsed sections

cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:@"ExpandableAccessoryView"]];

UIView *accessoryView = cell.accessoryView;

if ([[tableView indexesForExpandedSections] containsIndex:section]) {

accessoryView.transform = CGAffineTransformMakeRotation(M_PI);

} else {

accessoryView.transform = CGAffineTransformMakeRotation(0);

}

return cell;

}

// The next two methods are used to rotate the accessory view indicating whjether the

// group is expanded or now

- (void)tableView:(ExpandableTableView *)tableView willExpandSection:(NSUInteger)section {

UITableViewCell *headerCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

[UIView animateWithDuration:0.3f animations:^{

headerCell.accessoryView.transform = CGAffineTransformMakeRotation(M_PI - 0.00001); // we need this little hack to subtract a small amount to make sure we rotate in the correct direction

}];

}

- (void)tableView:(ExpandableTableView *)tableView willContractSection:(NSUInteger)section {

UITableViewCell *headerCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

[UIView animateWithDuration:0.3f animations:^{

headerCell.accessoryView.transform = CGAffineTransformMakeRotation(0);

}];

}

// Override to support conditional editing of the table view.

- (BOOL)tableView:(ExpandableTableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;

}

- (BOOL)tableView:(ExpandableTableView *)tableView canEditSection:(NSInteger)section {

return YES;

}

// Override to support editing the table view.

- (void)tableView:(ExpandableTableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

[tableView beginUpdates];

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete the row from the data source

[[_dataModel objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];

// cellVisibleForIndexPath: isn't strictly required sicne the table view will determine if the

// the row at that indexPath is actually visible, and do the appropriate manipulation

if ([(ExpandableTableView *)tableView cellVisibleForIndexPath:indexPath]) {

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

if ([[_dataModel objectAtIndex:indexPath.section] count] == 0) {

[_dataModel removeObjectAtIndex:indexPath.section];

[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

} else {

[tableView reloadSectionCellsAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

}

}

else if (editingStyle == UITableViewCellEditingStyleInsert) {

// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

}

[tableView endUpdates];

}

// Override to support rearranging the table view.

- (void)tableView:(ExpandableTableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

}

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(ExpandableTableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

// Return NO if you do not want the item to be re-orderable.

return YES;

}

- (BOOL)tableView:(ExpandableTableView *)tableView canRemoveSection:(NSUInteger)section {

return YES;

}


@end

1 个答案:

答案 0 :(得分:0)

有一个名为:_ungroupSingleElement的ivar将其设置为false然后禁用此功能

相关问题