繁体   English   中英

UIView中的UITableViewController

[英]UITableViewController in UIView

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

我有一堂课:

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

我有AppDelegateViewController.xib文件。

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

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

我会创建一个uiview并在uiview(rootViewController)中加载uitableview,上面说的是,我假设您使用的是界面生成器(IB),它使外观更直观并且更易于视觉操作。 如果需要,我将添加相关代码。

您可以推送UITableViewController或包含UITableView的UIViewController

或juse将tableView添加到UIViewController的当前视图

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

将此代码写入.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];
   }

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

如果在viewcontroller的视图中除了tableview外没有其他内容,则也可以使其成为UITableViewController的子类。

您可以在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);
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM