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.
Related
I have created two types of prototype cells in storyboard. The dimension of one of them have been customized to accomodate UIButton object. However when the cells are created, they have the standard height. I can see the UIButton object but it gets truncated because of the cell height.
Why are the newly created cells different from the prototype cells?
The relevant section of the code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(cell == nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"PictureSelectionCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
pictureButtonProperty = (UIButton *) [cell viewWithTag:1];
}
}
Going forward, what are my options for creating the cell of the width (or dimensions) defined in the storyboard? Programmatically, I will be able to achieve this by creating a CGRect object with the specified dimensions and then create a cell using initWithFrame. However, I would like to avoid doing things manually.
Thanks for your response.
first of all you can always set it with code
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YOUR_ROW_HEIGHT;
}
other way if you choose your UITableView if the storyboard, under the size inspector change the Row Height.
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 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.
I have UITable which contains some UIButtons. I would like a way to use the label.
The problem I have is that I need two tag labels, one for retrieving the cell in tableviewCellWithReuseIdentifier and configureCell.
Until recently I have been using the UIButton.title to determine which row in the table I selected. The (invisible) text was the row number.
Now I need to use the title for some visible text. How can I still find which row was pressed?
When I press the area outside the button the usual didSelectRowAtIndexPath is called. The hard part is that I want to capture both the UIButton (which calls its own selector) and the row, which I had been finding out from the title. Now I need the title for something else.
I use something like this. This will get you the indexpath in the tableview.
- (IBAction)buttonAction:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];
// do something
}
You could subclass UITableViewCell or UIButton and add some properties.
You should implement the UITableViewDelegate protocol.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do something based on selected row (indexPath.row)
}
Implement the UITableViewDelegate protocol. It will help you and do what you need.
didSelectRowAtIndexPath is autmatically called whenever a row is selected in your table.
in your .m file the one with the table view controller if there isn't one there add this method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSInteger row = [indexPath row];
// or (indexPath.row)
// do whatever you like with this value which tells you which row is selected.
}
PK
I want to add the content of the cell of tableview in other view when i click on the tableview cell , how can i pass the values of the cell to other view.
If you want use the text of a cellview for example you can do that in your UITableViewDelegate (or your UITableViewController):
// Tells the delegate that the specified row is now deselected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *contentCell = [targetCell textLabel];
// Do something with the contentCell
}
In your table view delegate (by default, it's the table view controller) implement the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Use indexPath as you did in -tableView:cellForRowAtIndexPath: to get the values you need. To avoid unexpected behavior you should extract the values from your model rather than the cell.