簡體   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