【发布时间】:2014-11-29 00:47:45
【问题描述】:
我正在尝试动态填充带有按钮行的表格。启动表时,我将其传递给包含正确按钮的数组。按钮的数量以及行的数量会有所不同。这有效,直到我需要创建一个新行(即第一行的按钮太多)。假设我将其限制为每行 4 个按钮。然后我的按钮从第 2 行开始,而不是应该的第 1 行。它们也会被剪裁在正确的边界上。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = 0; // No rows if section is unknown!
switch (section)
{
case TableViewSectionFormatButtons:
{
if ([[self fileTypeButtonsArray] count] % kButtonsPerRow)
return ([[self fileTypeButtonsArray] count] / kButtonsPerRow) + 1;
else
return [[self fileTypeButtonsArray] count] / kButtonsPerRow;
}
break;
case TableViewSectionExportSwitches:
rows = 4;
break;
case TableViewSectionExportButton:
rows = 1;
break;
default:
break;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *defaultCellIdentifier = @"defaultCellIdentifier";
static NSString *detailCellIdentifier = @"detailCellIdentifier";
UITableViewCell *cellToReturn = nil;
// Usage note: Both kinds of cells get created every time, which is arguably wasteful. Since there aren't many rows involved
// it's simpler just to ignore the one that we don't need. Assign the one we want to cellToReturn.
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier]; // Image on left
if (defaultCell == nil) {
defaultCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentifier];
}
UITableViewCell *detailCell = [tableView dequeueReusableCellWithIdentifier:detailCellIdentifier]; // Text on right
if (detailCell == nil) {
detailCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:detailCellIdentifier];
}
// Clear old values
defaultCell.textLabel.textAlignment = NSTextAlignmentLeft;
defaultCell.accessoryType = UITableViewCellAccessoryNone;
defaultCell.accessoryView = nil;
defaultCell.imageView.image = nil;
detailCell.accessoryType = UITableViewCellAccessoryNone;
detailCell.imageView.image = nil;
switch (indexPath.section) {
case TableViewSectionFormatButtons: {
for (int i = 0; i < [self.fileTypeButtonsArray count]; i++)
{
UIButton *currentButton = (UIButton *)[self.fileTypeButtonsArray objectAtIndex:i];
[currentButton setTag:i];
[currentButton setFrame:CGRectMake((kButtonPadding + (i * (kButtonWidth + kButtonPadding))), kButtonPadding, kButtonWidth, kButtonHeight)];
[defaultCell.contentView addSubview:currentButton];
}
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
cellToReturn = defaultCell;
}
break;
case TableViewSectionExportSwitches: {
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
defaultCell.textLabel.text = NSLocalizedString(@"synopsisSwitchLabel", @"Synopsis - Export switch label to indicate if Synopsis text should be included in export.");
self.synopsisSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
defaultCell.accessoryView = self.synopsisSwitch;
// self.synopsisSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsShowNotesIndicatorKey];
[self.synopsisSwitch addTarget:self action:@selector(synopsisSwitchValueChanged:) forControlEvents: UIControlEventValueChanged];
}
else if (indexPath.row == 1)
{
defaultCell.textLabel.text = NSLocalizedString(@"bodySwitchLabel", @"Body - Export switch label to indicate if Body text should be included in export.");
self.bodySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
defaultCell.accessoryView = self.bodySwitch;
// self.bodySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsShowNotesIndicatorKey];
[self.bodySwitch addTarget:self action:@selector(bodySwitchValueChanged:) forControlEvents: UIControlEventValueChanged];
}
else if (indexPath.row == 2)
{
defaultCell.textLabel.text = NSLocalizedString(@"notesSwitchLabel", @"Notes - Export switch label to indicate if Notes should be included in export.");
self.notesSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
defaultCell.accessoryView = self.notesSwitch;
// self.notesSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsShowExpandedOutlineKey];
[self.notesSwitch addTarget:self action:@selector(notesSwitchValueChanged:) forControlEvents: UIControlEventValueChanged];
}
else if (indexPath.row == 3)
{
defaultCell.textLabel.text = NSLocalizedString(@"imagesSwitchLabel", @"Images - Export switch label to indicate if Images should be included in export.");
self.imagesSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
defaultCell.accessoryView = self.imagesSwitch;
// self.imagesSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsStoryboardModeKey];
[self.imagesSwitch addTarget:self action:@selector(imagesSwitchValueChanged:) forControlEvents: UIControlEventValueChanged];
}
cellToReturn = defaultCell;
}
break;
case TableViewSectionExportButton:
{
defaultCell.textLabel.textAlignment = NSTextAlignmentCenter;
if (indexPath.row == 0)
{
defaultCell.textLabel.text = NSLocalizedString(@"nextButtonTitle", @"Next - Button title indicating user is ready to proceed with Export.");
defaultCell.textLabel.textColor = [UIColor blueColor];
}
cellToReturn = defaultCell;
}
break;
}
return cellToReturn;
}
示例按钮超过 1 行 - 错误
示例按钮适合 1 行 - 好
我意识到我只是在告诉它继续在彼此的右侧添加按钮。我很难过如何告诉它将它们放入哪些行。也很难过为什么它们从第二行(索引 1)而不是第一行(索引 0)开始。感谢您的帮助。
【问题讨论】:
-
可以编辑您的问题以包含您的整个 cellForRowAtIndexPath 方法吗?
-
当然。它很大,里面有一些不相关的东西,但如果有帮助的话......
标签: ios ipad uitableview