Как я добавляю UIButton или UISwitch в tableView:viewForFooterInSection

Обычная array распадается на указатель на свой первый элемент, он равен &array[0]. Первый элемент также начинается с того же адреса, что и сам массив. Отсюда &array == &array[0].

Но важно отметить, что типы различны:

  • Тип &array[0] (в вашем примере) int*.
  • Тип &array - int(*)[5].

Отношения между &array[0] и &array могут быть проще, если я покажу это немного «графически» (с добавленными указателями):

+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array

Вещи разные с указателями, хотя. Указатель pArray указывает на некоторую память, значение pArray - это местоположение этой памяти. Это то, что вы получаете, когда используете pArray. Это также как &pArray[0].

Когда вы используете &pArray, вы получаете указатель на указатель . То есть вы получаете местоположение (адрес) самой переменной pArray. Его тип int**.

Несколько графически с указателем pArray было бы что-то вроде этого

+--------+       +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+       +-----------+-----------+-----------+-----------+-----------+-----+
^                ^
|                |
&pArray          &pArray[0]

[Обратите внимание на ... в конце «массива», потому что указатели не сохраняют информацию о памяти, на которую он указывает. Указатель указывает только на определенное место, «первый» элемент «массива». Обрабатывать память как «массив» - дело программиста.]

6
задан Kevin Bomberry 8 May 2009 в 04:38
поделиться

2 ответа

Хорошо, после поиска и работы я сделал следующее:

// Need to refactor so that the label is Public Sharing and Priviate Sharing and the actions work for each switch
- (UIView *) tableView: (UITableView *) tableView
viewForFooterInSection: (NSInteger) section
{
  if (section == 0 || section == 1) {
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    UIView* footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)] autorelease];
    footerView.autoresizesSubviews = YES;
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    footerView.userInteractionEnabled = YES;

    footerView.hidden = NO;
    footerView.multipleTouchEnabled = NO;
    footerView.opaque = NO;
    footerView.contentMode = UIViewContentModeScaleToFill;

    // Add the label
    UILabel*    footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(150.0, -5.0, 120.0, 45.0)];
    footerLabel.backgroundColor = [UIColor clearColor];
    footerLabel.opaque = NO;
    footerLabel.text = @"Sharing";
    footerLabel.textColor = [UIColor tableHeaderAndFooterColor];
    footerLabel.highlightedTextColor = [UIColor tableHeaderAndFooterColor];
    footerLabel.font = [UIFont boldSystemFontOfSize:17];
    footerLabel.shadowColor = [UIColor whiteColor];
    footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
    [footerView addSubview: footerLabel];

    [footerLabel release];  

    // Add the switch
    UISwitch* footerSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(215.0, 5, 80.0, 45.0)];
    [footerView addSubview: footerSwitch];

    // Return the footerView
    return footerView;
  }
  else return nil;
}
// Need to call to pad the footer height otherwise the footer collapses
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  switch (section) {
    case 0:
    case 1:
      return 40.0;
    default:
      return 0.0;
  }
}

Я надеюсь, что это правильно, и если это кому-то поможет, проголосуйте за это. Ура!

8
ответ дан 16 December 2019 в 21:45
поделиться

Я думаю, вам нужно автоматически выпустить uiview-

0
ответ дан 16 December 2019 в 21:45
поделиться