1

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

我有一堂课:

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

我有AppDelegate,ViewController.xib文件。

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

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

4

5 回答 5

2

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

于 2012-04-18T08:32:25.477 回答
2

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

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

UITableView tableView = [[UITableView alloc] initWithFrame:self.view.bound];
[self.view addSubView:tableView];
于 2012-04-18T08:33:15.433 回答
2

将此代码写入 .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];
   }
于 2012-04-18T08:37:30.027 回答
1

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

如果在您的 viewcontroller 的视图中除了 tableview 之外什么都没有,那么您也可以将其设为 UITableViewController 的子类。

于 2012-04-18T08:31:26.610 回答
0

您可以在 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);
   }
于 2012-04-18T08:40:19.873 回答