【问题标题】:UITableView add buttons in sectionUITableView 在部分中添加按钮
【发布时间】:2011-04-02 04:04:58
【问题描述】:

我是 iOS 编程新手。我正在为课程管理系统构建一个应用程序,该系统具有一个用于导航以在不同课程之间导航的表格视图。我想向表格的每个部分添加按钮而不是行。

例如,如果 CS101 课程下面有 5 个项目,我希望在单击名为 CS101 的标题时出现 5 个图标(UIButton),它们排列成 3 行,2,2 和 1 个按钮代表这五个项目。

我该怎么做呢?如果可以的话?

非常感谢! 萨蒂扬

【问题讨论】:

    标签: ios uitableview uibutton


    【解决方案1】:

    是的,有可能。你需要使用 UITableView 的委托方法,并将其中包含所有 Button 的 View 添加到 TableView 的标题中。

    首先设置 UITableView 的 Delegate & Datasource 然后继续下面的代码。

    为此,您可以按照以下代码:

     - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
     {
     // create the parent view that will hold header Label
     UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    
     // create the button object
     UIButton * headerBtn = [[UIButton alloc] initWithFrame:CGRectZero];
     headerBtn.backgroundColor = [UIColor clearColor];
     headerBtn.opaque = NO;
     headerBtn.frame = CGRectMake(10.0, 0.0, 100.0, 30.0);
     [headerBtn setTitle:@"<Put here whatever you want to display>" forState:UIControlEventTouchUpInside];
    [headerBtn addTarget:self action:@selector(ActionEventForButton:) forControlEvents:UIControlEventTouchUpInside];
     [customView addSubview:headerBtn];
    
     return customView;
     }
    

    一旦视图被添加到页眉,然后设置页眉的高度,因为默认情况下它会是 20.,所以您需要根据已添加到页眉的视图高度进行调整。

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
     {
     return 100.0;
     }
    

    希望这对你有很大帮助... :)

    【讨论】:

    • 感谢帮助!我将 UITableViewCell 子类化并使用界面构建器创建了我自己的带有两个按钮的表格单元格,然后使用了 UITableViewDelegate 和 DatasourceDelegate 的大量委托。
    【解决方案2】:

    为此,您必须在此委托中返回一个 UIview,其中包含一个 UIbutton

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    

    在此按钮的操作上,您可以插入要显示为图标的行数,新插入的行将包含一个 UIButton。对于插入行,您可以使用

    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      相关资源
      最近更新 更多