2014-11-29 6 views
0

Я пытаюсь динамически заполнять таблицу динамическими строками. Когда таблица запущена, я передаю ей массив, содержащий правильные кнопки. Количество кнопок, и, следовательно, строк, будет отличаться. Это работает, пока мне не понадобится создать новую строку (т. Е. Слишком много кнопок для установки в первой строке). Предположим, я ограничу его 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 строку - плохая enter image description here

Пример кнопка помещается на 1 ряд - хорошая enter image description here

Я понимаю, что я просто говорю это, чтобы добавлять кнопки справа друг друга. Я в тупике, как я рассказываю, в какие строки их вставлять. Также тупик, почему они начинаются во второй строке (индекс 1) вместо верхней строки (индекс 0). Спасибо за любую помощь.

+0

Может редактировать свой вопрос, чтобы включить весь метод cellForRowAtIndexPath? –

+0

Несомненно. Он довольно большой, с некоторыми несвязанными вещами, но если это помогает ... – DenVog

ответ

1

В способе numberOfRowsInSection: ваши TableViewSectionFormatButtons: накладываются на дополнительную строку, если кнопки аккуратно вставляются в строку, то есть if ([[self fileTypeButtonsArray] count] % 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; 
} 

Нечто подобное должно работать.

P.S. Тот факт, что вы оба повторно используете ячейки и добавляете кнопки в качестве подзонов во время каждого cellForRowAtIndexPath:, вероятно, приведет к некоторым проблемам в будущем. Если вы собираетесь очистить старые значения таким образом, что вы уже сделали в своем коде, возможно, вы также должны удалить старые кнопки в том же самом месте, исключая:

// 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]; 
    } 
} 
+0

Спасибо за ваш продуманный и подробный ответ. Вы абсолютно правы в отношении numberOfRowsInSection. Это давало мне желаемые результаты, но не по правильной причине. – DenVog

+0

cellForRowAtIndexPath сбой при передаче массива, где количество кнопок больше, чем разрешенных кнопок в строке. Завершение приложения из-за неотображенного исключения «NSRangeException», причина: «*** - [__ NSArrayM objectAtIndex:]: индекс 5 за пределами границ [0 .. 4]. Я полагаю, что проблема заключается в этой строке: for (int i = firstButtonIndex; i DenVog

+0

@DenVog Да, вы правы. Я обновлю свой ответ, включив этот случай. –

Смежные вопросы