简体   繁体   中英

UITableViewController in UIView

I have a plain project with blank UIView . Later, when I'm get some useful information from my serwer i would like to create UIView or UITableView .

I have a class:

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

And I have AppDelegate , ViewController and .xib file.

What i should do to load a UITableView ? I should unload my standard ViewController in AppDelegate or Load TableView inside View as a Subview (possible?) Or i should throw out xib file at all ? Of course everything i want to do in to my Views has to be programically not using IB.

In my app i want to create both, depends on receiving data.

I would create a uiview and load the uitableview inside the uiview (rootViewController) with that said, I assume you are using interface builder (IB) which makes things much more visual and easy to manipulate visually. If needed I will add the relevant code.

you can push a UITableViewController Or a UIViewController contain a UITableView

or juse add a tableView to current view of UIViewController

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

write this code in .h file

     @interface SelectPeopleViewController : UIViewController <UITableViewDelegate,  UITableViewDataSource> {

      UITableView *tableView;

       }

       @property (nonatomic, retain) UITableView *tableView;

than add datasource and delegate of Uitableview to your File's Owner

write this code in .m file

          #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];
   }

Yes you can add UITableView to an UIViewController's UIVIew if there is a necessity to add other controls to a view.

If there is nothing other than a tableview in your viewcontroller's view then you can make it an subclass of UITableViewController as well.

You can use tableView in your UIViewController

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

Then in your viewController xib file create a tableview in view. connect it to the mytableview in files owner.Also DONT FORGET to set delegate and data source to files owner.

Later in your .m file create these functions and modify it according to your data.

   - (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);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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