【问题标题】:How do I add a UIButton or UISwitch in tableView:viewForFooterInSection如何在 tableView:viewForFooterInSection 中添加 UIButton 或 UISwitch
【发布时间】:2009-05-08 04:38:49
【问题描述】:

我正在尝试了解如何将带有 UISwitch 或其他控制器的标签添加到分段 tableView 中的页脚(或页眉)。任何帮助将不胜感激。提前谢谢!

【问题讨论】:

    标签: objective-c iphone cocoa-touch uitableview uikit


    【解决方案1】:

    好的,在搜索和处理之后,我做了以下事情:

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

    我希望这是正确的,如果这对其他人有帮助,请投票。干杯!

    【讨论】:

      【解决方案2】:

      我认为你需要自动释放 uiview-

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多