When someone selects a cell in a collection view (about 15 cells) inside the
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
method I am trying to change a label I have placed in the one header by using
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"Header" forIndexPath:indexPath];
UILabel *headerTitle=(UILabel *)[headerView viewWithTag:1];
headerTitle.text=#"test";
the tag and everything is set correctly, but it does not change. Any thoughts on where I'm going wrong?
You already have a header view, so dequeuing one creates another one which is not the one you're trying to change. There is no way to access a header view in a collection view -- there's no headerForSection: method, so you have to change the label through your data source. So, if you just have the one header, then you should have a string property, lets call it headerTitle, that you use to populate the label in the header. So, your implementation of collectionView:viewForSupplementaryElementOfKind:atIndexPath: should look something like this:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
RDReusableHeader *headerView;
if (kind == UICollectionElementKindSectionHeader){
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"MyView" forIndexPath:indexPath];
headerView.label.text = self.headerTitle;
}
return headerView;
}
Then in didSelectItemAtIndexPath:, assign a new value to that property and call reloadData on the collection view.
self.headerTitle = #"This is the New Tilte";
[self.collectionView reloadData];
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 am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.
I unfortunately still have not seen the light when it comes to organising my iphone app nicely into controllers and views. Let me illustrate with an example:
I am working on a sign up page which consists of a table view with a list of custom table cells. Some of these cells have a text field inside them and when the user touches one of those a keyboard slides up from the bottom. The keyboard has a return key in its lower right corner and when the user hits this key I would like the keyboard to slide down again.
Now, where do I put the
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
? Currently I have made my custom table cell conform to the text field delegate protocol and have put the method in there, but it does seem a bit wrong to have stuff like that inside a view class? On the other hand I do not find it appropriate in the table view controller either.
you can set your table view controller as the text field's delegate...
just remove the code in the custom cell where you set it as the delegate and instead set the delegate in the table view controller's cellForRowAtIndexPath method where you actually create and return the cell..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
MyCustomCell *myCell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil)
{
myCell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:CellIdentifier] autorelease];
myCell.myTextField.delegate = self;
}
//other cell specific code goes here
return myCell;
}
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-----
I have a uitableview that is populated from a sqlite query.
I want to select or click on a row and then display that row's value in a uilabel field. To show the user that the row was selected.
I also want to pass that value on to different controllers that will be called later.
Here is a copy of my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"psystem";
PSystem *psystem = [self.ppdm_systems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
// self.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.text = psystem.system_id;
return cell;
}
I took out the _label.text .... in my various experiments.
Now what is not working is the passing of the value to different controllers.
Using the example listed here, the source controller is TableViewController and is where the value is set. The target controller is DetailViewController.
I can pass the title of the tab bar in, but that's from TableView --> DetailView.
I am not sure how to pull from tableview; ie: Tableview <-- DetailView when I am in DetailView.
thx
In your UIViewController, implement:
- (MyObject *)valueForSelectedRow {
MyCell *cell = (MyCell *)[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
return cell.myObject;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get value
MyObject *object = [self valueForSelectedRow];
// Update the label, assumed that _label is a pointer to a UILabel view object.
_label.text = object.myValue;
}
When you want to push a new view controller, you just call -valueForSelectedRow and then use that value to push the controller.
This is assumed that you have a UITableViewCell subclass, with a property set to some model object. When you don't have that and just set the text property, that NSString object will be your 'model' object, although it would be easier when your cells handle custom model objects.
EDIT: Thanks for editing your answer. I now have the information I need. In this line: cell.textLabel.text = psystem.system_id, you setup the cell by simply setting the textLabel's text property. This is what I described in the paragraph above. I always create a UITableViewCell subclass, with a property set the the complete PSystem object. When you assign a PSystem object to the cell, it will handle it's contents, so you can easily manage your view in the, well, view. That's a very compelled approach since you never have to look at the controller again to alter the view's contents.
However, it can be done the way you currently have it. It would look something like:
- (NSString *)valueForSelectedRow {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
return cell.textLabel.text;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get value
NSString *value = [self valueForSelectedRow];
// Update the label, assumed that _label is a pointer to a UILabel view object.
_label.text = value;
}
In this case, your PSystem model has been replaced with an NSString object. For this, it's enough, but it could be so much easier to have the object itself. Okay, that can also be done by selecting the PSystem object again from the p_system array by the NSIndexPath, but things will become harder once you come up with more complex tableviews.