I now want to refer to the table, but there isn't a variable in my .h file as it wasn't required when originally created. The table works great, can add/display/etc.
I tried adding "myTable" to the .h and then linking in IB (the table was linked to "view" and I c开发者_如何转开发ould/did select "myTable"), but my first try didn't work and I was afraid of messing up my (somewhat) working app ;-)
Hope the question makes sense!!!
thx!
Simply use
IBOutlet UITableView * myTableView;
in your .h file i.e Declaration file.You can now access myTableView in IB.You can now create the object for this TableView.
If I understand you correctly, you want to link the table with your code. If that's the case, then in your header file you need to add a property like so
@property(nonatomic, retain) IBOutlet UITableView *myTable;
And then synthesize it in your .m file like so
@synthesize myTable;
Once you've done that you will be able to link it up in interface builder. But bare in mind that if you want to populate/interact with the table further you need to set the data source/delegate of your table to your file's owner. Here is a tutorial on how to do that - http://www.switchonthecode.com/tutorials/how-to-create-and-populate-a-uitableview
Does that answer your question?
If your controller is a UITableViewController
, just use its tableView
property.
The question doesn't entirely make sense because it's not clear what "the table works great..." means if you don't have any reference to it in your .h file. I'll assume you do have an iVar of class UITableView that is defined as an IBAction and you've implemented the table view dataSource and delegate protocols in your view controller implementation. If you have not then the table cannot display any data.
First, if you have an iVar defined you can just refer to the iVar directly. If you prefer a property you can create one.
If your view controller is a UITableViewController you don't need to do anything, the property tableView
is already defined for you and you can refer to the table with the tableView
property.
If your view controller is something else, you can create a property as follows in your .h file:
@property (nonatomic, retain) UITableView * myTableView;
and in your .m
@synthesize myTableView;
You can then refer to your tableView with:
self.myTableView;
Replace myTableView
with the name of your tableView iVar
. If you need to connect your tableView in IB to the myTableView property then instead define the property as:
@property (nonatomic, retain) IBOutlet UITableView * myTableView;
Once you've done this, edit your UI in IB and connect the tableView outlet to the myTableView
property. You will also need to assign the tableView dataSource and delegate correctly. This is getting a bit beyond your question. Apple's Table View Controller Programming Guide has great docs on this.
精彩评论