Access parent view from custom cell - iphone

How can I access the UIView in which I have a UITableView, from the custom cells inside that table. I cant find a method to do that.
Thanks

You can add an instance variable that points to the UITableView and set it when creating/configuring the cell (e.g. in tableView:cellForRowAtIndexPath:). Make sure that your cell does not retain the tableView though.
Knowing your cell’s tableView, call [parentTableView superView] to access the UITableView’s parent view:
#interface PropertyListingCell : UITableViewCell {
__weak id parentTableView;
}
- (void) setParentTableView:(UITableView*)tv; // parentTableView = tv;
In UITableViewController implementation:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//dequeue/create and configure your custom cell here
[cell setParentTableView:tableView];
return cell;
}
UPDATE:
If you're using recent XCode (at least 4.3) you can simply add
#property (weak) UITableView *parentTableView; // use unsafe_unretained instead of weak if you're targeting iOS 4.x
to the #interface section of your UITableViewCell's subclass. Then, when you're creating a cell (in tableView:cellForRowAtIndexPath:) set this property accordingly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
//dequeue/create and configure your custom cell here
// ...
cell.parentTableView = tableView;
return cell;
}
And in your cell class call self.parentTableView to access the tableView this cell belongs to.

Related

iOS 7 UITableView indexpath row property is nil

I have a UITableView and it contains a custom UITableViewCell. To test, I have an array that has three strings in it. The UITableView delegate methods are called as expected, however, the tableView:cellForRowAtIndexPath delegate is always passed an NSIndexPath instance whose row property is always == nil:
tableView:cellForRowAtIndexPath is called 3 times (once for each object in my array). I added the tableView from within the designer (storyboard) and created an outlet for it. The UITableViewCell instances appear to be correctly instantiated which each call to this delegate. I just can't wrap my head around why the [indexPath row] value is always nil.
Interface(s):
In the implmentation file:
#interface FirstViewController ()
#property(nonatomic, strong)AppDelegate *sharedDelegate;
#property(nonatomic, strong)NSArray *userList;
#end
In the header:
#interface FirstViewController : UITableViewController <FacebookDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
Init the custom cell:
-(void)viewDidLoad
{
[self.tableView registerClass: [ListCategoryCell class]forCellReuseIdentifier:#"ListCategoryCell"];
self.userList = #[#"d", #"g", #"f"]; // make some test data
}
And the delegate this is driving me mad:
//NSIndexPath.row is nil ?!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"ListCategoryCell";
ListCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = (ListCategoryCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell.titleLabel.text = [self.userList objectAtIndex:[indexPath row]];
cell.detailLabel.text = #"Detail";
return cell;
}
Am I missing something? Thanks!
Working Now
I left out some context (and I should not have) that I believe was very relevant to my problem. I created a UIViewController originally and then added a UITableView to this as the view. In the UITableView I created a custom prototype cell. I did all the house work:
UIViewController implemented the UITableViewDelegate & UITableViewDatasource.
Created and outlet for the UITableView.
Hooked up all the outlets
Everything seemed to work except for the fact that indextPath.row property was always nil. Some resources I found suggested that custom cells were not visible before the uitableview delegates were called.
In the end I made my class a subclass of UITableViewController. Things started working. I am still curious why my original attempt was failing.
Thanks for everyone's time. Some great comments helped me investigate some topics that are "good to know".
You need to provide at least two methods in view controller if you want it to manage your table view. They are:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You've already provided the second, so your table view actually can produce cells but it doesn't know how many.The default value the first method returns is nil.That is the reason why you don't even have an index path.
Optionally:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
The default value is one,so you don't need to override this in case you have only one section
Make sure your view controller also follows delegate and datasource protocols.

How to customize programmatically uitableviewcell subclass, which has a nib?

I am using custom UITableViewCell with a nib file with name "MyCellNib",for example.
I my tableviewcontroller i register in viewDidLoad this nib like this:
[_table registerNib:[UINib nibWithNibName:#"MyCellNib" bundle:nil]
forCellReuseIdentifier:#"cell"];
in my cellForRowAtIndexPath: it looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
cell = [_table dequeueReusableCellWithIdentifier:#"cell"];
cell.clipsToBounds = NO;
return cell;
}
As u see i dont use standart pattern like if(cell == nil) etc.(as we do it in iOS4) - regiserNib method can do it for us.
So, the problem: I need to write some code in the cell subclass.
in my custom uitableviewcell subclass the method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
is not called (i used it before to customizw the cell), but i need it to add some stuff on contentView programmaticaly..
Is there any way to customize my custom cell when it is created?
Figured it out.
All this pre-stuff can be performed in -(void)awakeFromNib method

UITableView adding cells dynamically

I have a UITableView in my nib file and would like to dynamically add some content to each of the cells in that TableView. Is there a way to do this? I have an array of text that I would like to display in the TableView as well as an array of Pictures.
You need to implement the UITableViewDataSource protocol and override the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
methods. You will want to return the length of your array for the tableView:numberOfRowsInSection: method. In the tableView:cellForRowAtIndexPath: method you will get create the UITableViewCell (dequeue one if available) and add the UIViews you want to hold your data (i.e. a UIImage, etc.). Access the data to populate your view using the indexPath.row as the index to your array(s). Does that all make sense? It sounds a bit more complicated than it is in practice.
Here is the documentation for the UITableViewDataSource protocol
My ideal is to register as an observer for each cell, then the interested content has changed, then it sends event or necessary data to those cells. By comparing some information, like current indexPath, or some unique identifier of cell, the cell can decide to accept those sent data and change himself, or just pass this sent event and data.
I has implemented above for loading thumbnail image in the background, when the image has been loaded, it notify those cells to update images. And if any source data has been modified, it will notify the those registered cells, then those cells will reload necessary data to update their content.
If you want add cell and data on that cell dynamically from one view controller to other Tableview controler----
Step--1:[[NSNotificationCenter defaultCenter] postNotificationName:#"ReturnTableView" object:(send array object or any object)];
Step--2:go to your Table View controller
Step--3:In YourTableView.h file add this method : - (void)change_view:(NSNotification *)notif;
Step--4:Now Came in YourTableView.m file add the line in viewDidLoad ---[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(change_view:) name:#"ReturnTableView" object:nil];
Step--5:Now Add the method in YourTableView.m ----
- (void)change_view:(NSNotification *)notif {
if([[notif name] isEqualToString:#"ReturnTableView"]){
Your Object Ref(Obj-1)=[notif object];
[Obj-1 addObjectsFromArray:self.AnotherMutableArray(obj-2)];
self.obj-2=Obj-1;
[self.tableView reloadData];
}
}
Step--6:Now Add Finally in
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifierName"];
UILabel *label;
label = (UILabel *)[cell viewWithTag:TagNo(e.g:0)];
label.text = [self.messageposts objectAtIndex:indexPath.row];
label = (UILabel *)[cell viewWithTag:TagNo(e.g:1)];
label.text = [self.messageposts objectAtIndex:indexPath.row];
return cell;
}
Now Your Data Is Added
Thanks-----

How can i add objects into UITableView?

How can i add objects into UITableView?
You have to first take IBOutlet Object of UITableView in interface file and then bind it using interface builder. Set delegate and data source of UITableView in the interface builder property.
And do the following coding in your project.
In .h file:
#interface RootViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *tblView;
}
In .m file:
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return n;// n for number of row.
}
// 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];
}
// Configure the cell.
return cell;
}
Implement the UITableViewDataSource delegate methods.
Also check out the Table View Programming Guide.
Tableviews are populated by datasources, datasources are often an array of objects.
I suggest you look through some tutorials on tableviews and also delegate methods.
http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/
icodeblog is a fantastic website for beginners.

A question related to cell of UITabelview

UIButton * button;
I have put cell.accessoryView = button;
In didSelectRowAtIndexPath i have written above code,but it doesn't show UIButton on cell of UITabelView
You need to put this in the delegate method -tableView:cellForRowAtIndexPath: that initializes and returns cells for the table view:
- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// initialize cell and contents here, for the specified indexPath value
}
You might want to read the Table View Programming Guide for iPhone OS, which explains this in detail.