objective-c - 如何在 tableView :viewForFooterInSection 中添加 UIButton 或 UISwitch

标签 objective-c iphone cocoa-touch uitableview uikit

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

最佳答案

好的,在搜索和处理之后,我完成了以下操作:

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

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

关于objective-c - 如何在 tableView :viewForFooterInSection 中添加 UIButton 或 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838266/

相关文章:

objective-c - 使 NSArray 全局化

iphone - 从 iOS 中的 Twitter 帐户获取关注者和关注者

ios - 我的 iOS 应用有多少次安装?

iphone - UIPageControl 不响应触摸,不改变点

iphone - 使用 UIProgressView 倒计时期间的事件处理

iOS屏幕一点点变黑

iOS 8 横向模式无法正常工作,应用程序不旋转

iphone - 如何在 URL 中转义 unicode?

ios - NSMergeConflict(两个线程) - 设置合并策略无法解决

Objective-C/Cocoa 相当于 fread