用代码写静态的cell

2年前 (2022) 程序员胖胖胖虎阿
171 0 0

iOSExamples-StaticCells
This project is an example for implementing a UITableView that has static cells. This pattern is often used on options pages or for creating/editing model objects.

The gist is that you can create a static number of cells and store them as variables. You can then use the tableView: cellForRowAtIndexPath method to retrive the appropriate UITableViewCell variable.

// Return the row for the corresponding section and row

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    switch(indexPath.section)
    {

      case 0:
      switch(indexPath.row)
      {
          case 0: return self.firstNameCell;  // section 0, row 0 is the first name
          case 1: return self.lastNameCell;   // section 0, row 1 is the last name
      }
      case 1:
      switch(indexPath.row)
      {
          case 0: return self.shareCell;      // section 1, row 0 is the share option
      }

    }
    return nil;
    }
    // Return the row for the corresponding section and row
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    switch(indexPath.section) {
    case 0:

      switch(indexPath.row) {
      case 0: return self.firstNameCell   // section 0, row 0 is the first name
      case 1: return self.lastNameCell    // section 0, row 1 is the last name
      default: fatalError("Unknown row in section 0")
      }

    case 1:

      switch(indexPath.row) {
      case 0: return self.shareCell       // section 1, row 0 is the share option
      default: fatalError("Unknown row in section 1")
      }

    default: fatalError("Unknown section")
    }
    }
    This example is implemented in both Objective-C and Swift. You can open the workspace to view both projects simultaneously.

版权声明:程序员胖胖胖虎阿 发表于 2022年9月15日 上午5:16。
转载请注明:用代码写静态的cell | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...