ios - 使用 Expandable UITableView 只显示一行

标签 ios iphone objective-c uitableview

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

enter image description here

我需要通过单击用户打开一个盘子和 DatePicker。为此,我使用 Expandable UITableView (http://www.wannabegeek.com/?p=338)。但我无法理解如何在该部分中这样做可能是一个元素(例如 DatePicker),并且通过单击该部分可以准确显示 DatePicker。现在,如果至少有两个元素,那么字符串不会显示为一个部分,而只是一个不包含更多元素的字符串。而且好像我没有改变原型(prototype)单元格的大小并没有改变我完全看不到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

最佳答案

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

关于ios - 使用 Expandable UITableView 只显示一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20494607/

相关文章:

ios - 报告 ALAssetRepresentation 元数据方法崩溃

ios - 我可以同时使用 google places API(自动完成)和 Mapkit 吗?

iphone - iPhone 上完全奇怪的字体渲染

ios - 我的按钮没有显示

iOS NSURLConnection 不从某些 URL 下载文件

ios - 从 UITableViewCell 内的 UITextField 中删除 Tap 手势

android - 哪种媒体格式更适合所有移动开发?

IOS:带有图案图像的颜色

iphone - dateformatter 在模拟器中有效,但在设备上无效

iphone - 上传离线数据和上传失败的数据