【问题标题】:Using Expandable UITableView to show only one row使用 Expandable UITableView 只显示一行
【发布时间】:2013-12-27 23:28:24
【问题描述】:

我尝试按照红色方块所示的图片进行操作。

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

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

在模拟器中:

可能会更容易做出决定?

我使用代码:

    // 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

【问题讨论】:

  • 你好。你解决了这个问题吗?

标签: ios iphone objective-c uitableview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多