我正在尝试动态地用多行按钮填充表格。当表格启动时,我向它传递一个包含正确按钮的数组。按钮的数量以及行的数量会有所不同。这是有效的,直到我需要创建一个新行(即太多按钮无法容纳在第一行)。假设我将它限制为每行 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)开始。感谢您的帮助。
最佳答案
在 numberOfRowsInSection:
方法中,您的 TableViewSectionFormatButtons:
如果按钮正好适合该行,则附加一行,即 if ([[self fileTypeButtonsArray]计数] % kButtonsPerRow)
;但这听起来与您想要做的完全相反。如果您的按钮无法整齐地放在其上方的行中,这听起来好像您只想添加一个额外的行,在这种情况下,我建议用一行代替:
case TableViewSectionFormatButtons: {
return ([[self fileTypeButtonsArray] count] + kButtonsPerRow - 1) / kButtonsPerRow;
}
简单地将整数除法四舍五入。
至于您的 cellForRowAtIndexPath:
方法,(1) 您需要考虑每行的按钮数量,即在您的算法中包含 kButtonsPerRow
,(2) 您需要知道您要添加到哪一行,并且 (3) 使用该信息您只想将属于当前行的按钮放在该当前行中,因此您不希望该循环从 0 开始然后继续一直到按钮数组的末尾。例如:
case TableViewSectionFormatButtons: {
// Calculate the index of the first button in the current row
int firstButtonIndex = indexPath.row * kButtonsPerRow;
// Then starting from that index place kButtonsPerRow within that row
for (int i = firstButtonIndex ; i < firstButtonIndex + kButtonsPerRow ; i++) {
if (self.fileTypeButtonsArray.count > i) {
UIButton *currentButton = (UIButton *)[self.fileTypeButtonsArray objectAtIndex:i];
[currentButton setTag:i];
[currentButton setFrame:CGRectMake((kButtonPadding + ((i-firstButtonIndex) * (kButtonWidth + kButtonPadding))), kButtonPadding, kButtonWidth, kButtonHeight)];
[defaultCell.contentView addSubview:currentButton];
}
}
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
cellToReturn = defaultCell;
}
类似的东西应该可以工作。
附言事实上,在每个 cellForRowAtIndexPath:
期间,您都重复使用单元格并将按钮添加为 subview ,这可能会导致一些问题。如果您要按照您在代码中已经完成的方式清除旧值,也许您还应该删除同一个地方的旧按钮,例如:
// Clear old values
defaultCell.textLabel.textAlignment = NSTextAlignmentLeft;
defaultCell.accessoryType = UITableViewCellAccessoryNone;
defaultCell.accessoryView = nil;
defaultCell.imageView.image = nil;
for (UIView *view in [defaultCell subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}
关于ios - 将按钮数组添加到适当的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197729/