【问题标题】:UITableViewController in UIViewUIView 中的 UITableViewController
【发布时间】:2012-04-18 08:27:14
【问题描述】:

我有一个空白UIView 的普通项目。后来,当我从我的服务器获得一些有用的信息时,我想创建 UIView 或UITableView

我有一堂课:

@interface MyTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
}

我有AppDelegateViewController.xib 文件。

我应该怎么做才能加载UITableView?我应该在 AppDelegate 中卸载我的标准 ViewController 或在 View 中加载 TableView 作为子视图(可能吗?)或者我应该扔掉 xib 文件?当然,我想对我的视图做的所有事情都必须以编程方式不使用 IB。

在我的应用中,我想创建两者,取决于接收数据。

【问题讨论】:

  • 是的,你可以做到,当从服务分配 uitableview 获取数据时,将它作为子视图集添加到你的 UIView 中,将 tableview 的委托给自己。

标签: iphone objective-c ios uitableview uiview


【解决方案1】:

我会创建一个 uiview 并在 uiview (rootViewController) 中加载 uitableview 话虽如此,我假设您正在使用界面构建器(IB),它使事物更加直观且易于视觉操作。 如果需要我会添加相关代码。

【讨论】:

  • 不,正如我所说的屏幕将取决于数据,所以我不能使用 IB。
  • 不确定“取决于数据”是什么意思。如果数据需要 tableview ,请将其加载到您的视图中并用数据(以编程方式)填充它,如果不需要 - 加载所需的内容。在 viewController 中加载 tableview 的原因是能够在 uitableview“周围”添加其他元素
  • 好的,所以如果我在我的 AppDelegate 中理解正确,我首先加载 UIView,然后将 UITableView 加载到其中。所以我什至不得不用 TableViewController 上课?
  • 是的,和阿达利代码一样,UITableView tableView = [[UITableView alloc] initWithFrame:self.view.bound]; [self.view addSubView:tableView];
  • 谢谢!这正是我所需要的!
【解决方案2】:

你可以推送一个 UITableViewController 或者一个 UIViewController 包含一个 UITableView

或者在 UIViewController 的当前视图中添加一个 tableView

UITableView tableView = [[UITableView alloc] initWithFrame:self.view.bound];
[self.view addSubView:tableView];

【讨论】:

    【解决方案3】:

    将此代码写入.h文件

         @interface SelectPeopleViewController : UIViewController <UITableViewDelegate,  UITableViewDataSource> {
    
          UITableView *tableView;
    
           }
    
           @property (nonatomic, retain) UITableView *tableView;
    

    将 Uitableview 的数据源和委托添加到文件的所有者

    将此代码写入 .m 文件中

              #pragma mark Table view methods
    
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
         return 1;
          }
    
       // Customize the number of rows in the table view.
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         return 5;
              }
    
          // Customize the appearance of table view cells.
         - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       static NSString *CellIdentifier = @"Cell";
    
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          }
    
    // Set up the cell...
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    cell.textLabel.text = [NSString  stringWithFormat:@"Cell Row #%d", [indexPath row]];
    
        return cell;
        }
    
             - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // open a alert with an OK and cancel button
         NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];
    [alert release];
       }
    

    【讨论】:

      【解决方案4】:

      是的,如果需要向视图添加其他控件,您可以将 UITableView 添加到 UIViewController 的 UIVIew。

      如果你的 viewcontroller 的视图中除了 tableview 之外什么都没有,那么你也可以让它成为 UITableViewController 的子类。

      【讨论】:

      • 谢谢,我只想澄清一件事。所以如果我在 UIView 中加载 UITableView 我不会得到编译器错误?在我的项目中,我得到了一个,所以我发现这是因为我尝试将 TableView 加载到 UIView 中,而不是 UIViewController。
      • UIViewController中不能加载UITableView,毕竟是添加到UIView中的。你能告诉我你收到的警告是什么吗?
      • 谢谢你,已经回应了我的错误;)现在如果我知道你在上面发帖,我可以处理它。
      【解决方案5】:

      你可以在你的 UIViewController 中使用 tableView

         @interface MyViewController:UIViewController<UITableViewDelegate, UITableViewDataSource>
         {
              IBOutlet UITableView *mytableview;
         }
          @property(nonatomic,retain) IBOutlet UITableView *mytableview;
      

      然后在您的 viewController xib 文件中在视图中创建一个 tableview。将其连接到文件所有者中的 mytableview。也不要忘记将委托和数据源设置为文件所有者。

      稍后在您的 .m 文件中创建这些函数并根据您的数据对其进行修改。

         - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
         {
             return 1; //how many sections in your tableView
         }
      
         - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:       (NSInteger)section
         {
      
             return [myArray count]; //how many rows you have
         }
      
         - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
             static NSString *CellIdentifier = @"MyCell";
      
             UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
             if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      
      // Configure the cell...
      
      cell.textLabel.text = @"title";
      
      return cell;
         }
      
         - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
         {
             NSLog("%d.row selected",indexPath.row);
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        相关资源
        最近更新 更多