Hide custom cell image - iphone

I am displaying a button in cutstom cell in UITableview. How do I hide that button when it is not needed. For example: I am display received images count on button. In case count will be zero, I need to hide that button from cell.
contactviewController.m
if (![[arr objectAtIndex:4] isEqualToString:#"0"]) {
[cell1 setImg:[arr objectAtIndex:4]];
}
Customcell.m
-(void)setImg:(NSString *)_text
{
imgView.titleLabel.textColor = [UIColor whiteColor];
[imgView setTitle:_text forState:UIControlStateNormal];
}

You can do this in your cellForRowAtIndex method. For each row creation this method is called.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(condition)
{
//show button
}
else
{
// don't show button
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(noImagesFound)
{
yourCustomCellObject.buttonObject.hidden = YES;
}
else
{
yourCustomCellObject.buttonObject.hidden = NO;
}
}

You can add buttons in cell via xib as IBOutlets or programmatically by setting frame and adding the button to the cell. The solution for above depends on how you have added the button. If you added it via xib as an outlet, you can either do one of the following.
There a property to any view called tag. You can set the tag value in the attribute inspector for the button and in the -cellForRowAtIndexPath: call as
UIButton *button =(UIButton*) [cell viewWithTag:9];
// your tag value (say 9)
and the use [button setHidden:YES] to hide //NO to unhide here you dont need a custom class
If you have created custom class for the same ,where you can add the button via two methods either by programmatically or via xib. If its xib, create a IBOutlet and make it a property of the custom cell so that you can get access to it using the custom cell object.
It's better to opt the second option if you need more control over the cell you created. If you had made it a property you can get access to it by using the object of the cell.

Create UITableViewCell.h and .m file.
Create some variable like UILabel and UIImageView object in files and make it IBOutlet and bind them with cell .xib file.
Inside UITableView implementation, and in "cellForRowAtIndexPath" you can use that custom UITableViewCell class object and use that synthesize variable of UILable and UIImageView and use it to show or hide accordingly.

Related

iOS cannot set UITextField text property in UITableViewCell

I have a UITableView of 'people' for my iOS app. I have created an 'add' screen to add new people to my persistent store, which works perfectly.
This add view uses a form created with a UITableView and subclassed UITableViewCells, with a UITextField and UILabel, which although works well, I am very new to iOS programming and feel that this may not be the most efficient way.
I am trying to re-use this add view to be my detail, add, and edit view, and I can successfully set a 'Person' entity as a property in the detail view. I have the following code in my viewDidLoad method, where the adding property is set in the prepareForSegue method in the previous ViewController :
if (self.adding)
{
self.editing = YES;
self.title = #"Add Person";
}
else
{
self.title = self.person.name;
[self setDetail];
}
My problem is that when I try to pre-populate my detail view's fields (my setDetail method), I am unable to set my UITextField text with the name property from my person entity. Here's the code I'm using to retrieve the UITextField and set it's text property with:
form is the UITableView;
UITableViewCell *nameCell = [form cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;
If I NSLog nameCell it returns (null)
I hope that's enough explanation. Any pointers would help a lot!
Instead of setting in viewDidLoad:, do it in your table view data source methods. i.e
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Deque the nameCell and prepare the cell if its not available.
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;
return nameCell.
}
I don't really understand your question, but cellForRowAtIndexPath may return nil because
Return Value
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
Official Document: cellForRowAtIndexPath:

Change a button dynamically in a table view cell

I have a table where 5 cells are there. In each cell there is a button. I need to change those buttons dynamically on some conditions. How to change the button dynamically in table view.
What you can do is Write a function which will change the values of the buttons and then you can call that function. After changing button values then use any one of the mentioned method below:
You can use either : [tableview reloadData]; to reload all the table data.
OR
You can reload particular rows using the following method :
[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];
You can query your table view for all visible cells through -visibleCells, get the reference of your button (assuming you have a UITableViewCell subclass with a property for the button) and change them.
First you need to set tags for each buttons. This can be done by
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:#selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];
}
Then you can get the button from this method
- (IBAction)buttonClick1:(id)sender {
int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:#selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];
//Your Code
}
You can follow the same steps for - (IBAction)buttonClick2:(id)sender
Now you can track which button was clicked and change the method for that button.
And declare your cell and button strong.

UIButton within a UITableViewCell to Perform a Segue to Another UIViewController Using Storyboard

In my App I have a ParentViewController that have several elements in it. One of the elements is a UITableView.
I designed the table's cells using a Dynamic Prototype Cell that I had designed within the Storyboard.
Meaning, that I have in the Storyboard a UIViewController; inside it I have a UITableView; and inside the UITableView I have a Prototype Cell.
Now, I added a UIButton inside the the Prototype Cell, created another UIViewController (let's call it ChildViewController) and connected the UIButton and the ChildViewController within the Storyboard with a Segue.
How should I write the code that activates the Segue when the user clicks the UIButton?
I managed to create the table itself and have data inside the Dynamic Cells (they have some UILabels aside to the UIButton), but I cannot figure out how to connect the UIButton to the code properly.
I don't want to use the UITableView default "select" features, since I wish to have several buttons inside the Dynamic Cells - each one will derive a different segue to a different ChildViewController.
Here's how I solved the same problem. Hope it will help.
To do this, you can pass the pointer to your UITableViewController to your custom cell's initialization method, and store it there:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
CDTrackCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellTableIdentifier];
CDTrack *currTrack = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell configureView:currTrack inTableViewController:self];
return cell;
}
Save the pointer in custom UITableViewCell class:
- (void) configureView:(CDTrack*)track
inTableViewController:(CDTracksViewController*)_tvc
{
self.tvc = _tvc;
// ...
}
Then, you can perform a segue when the button is tapped:
- (IBAction)buttonAddToTracklistTapped:(UIButton *)sender {
[self.tvc performSegueWithIdentifier:#"showAddToTracklist" sender:self];
}
Any better ideas?

Creating custom UITableViewCell's within a Storyboard

Wanting to create a static menu (IOS 5) and attempting to create custom cells within the storyboard to then load onto the grouped tableview.
I've created the outlet
#property(nonatomic,strong) IBOutlet UITableViewCell *labelCell;
The ViewController class is set to the proper TableViewController and I've connected the custom cell to this outlet.
I also have the delegate and datasource set up.
I've got
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.labelCell;
}
I'm sure there is a ton wrong with this, but I'm just trying to display one cell and go from there. There does not seem to be any examples of doing custom cells within the IB through the storyboard. I can still use the old way of creating a xib file and loading it in the mainBundle but I just want to stay up to date I guess.
but with what I have above i get a crash when I load this view controller. SIGABRT
Here is what I've learned about how you get cells for your table when using the storyboard. When you drag a UITableView into your view, it comes with a prototype cell already set as a subview. To use this prototype cell, set a unique reuse identifier in the attributes inspector, and then use the same identifier to dequeue the cell in your cellForRowAtIndexPath: method. I leave out the code for creating a cell from scratch if the dequeue call returns nil; I don't think it can happen. So just dequeue the cell, configure it with the usual UITableViewCell methods, and return it.
But you can also create custom subclasses of UITableViewCell. Just set the class name in the storyboard's class identity inspector, and drag whatever elements you want from the Objects palette into your cell. Then create IBOutlet properties for them in your subclass's code files, and hook them up to the cell in the storyboard in the usual way. This is so much better than having to do it all in code!
And finally, you can have more than one kind of cell in your table. Just drag UITableViewCell objects from the palette into the table, and give each one a unique reuse identifier in the attributes inspector. In your cellForRowAtIndexPath: method, choose the type of each cell and you can have a very flexible table view.
If you have set your UITableView to be using 'Static Cells' in the storyboard, you don't need to implement any of the UITableViewDataSource methods and you can modify the cell directly in Interface Builder. For a single label cell, select the cell and change it's type to 'Basic'. You can now edit the cell just like you would any other view object.
This tutorial was helpful to me. You can reference whatever object you need through the tag.
In the Storyboard drag on a UIImageView or UILabel etc. and set the tag to 100 (whatever you want) then in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath use the tag to reference it.
Here is the example code in the tutorial, just remember to set the tags in the storyboard:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;
return cell;
}

How to add buttons dynamically along with the scrollview in iPhone SDK?

In my iPhone app, I have to put buttons dynamically along with the scrollView.
Now I need to have button Click events associated with each of the buttons and perform a specific action on each button click.
My buttons have a title which is ASCII art. So creating common IBAction and performing the action based on the button title wont be a easy option in this case.
What can be the other options?
How can I associate the button click event with the specific button?
I think you can use the tag property of UIView.
My program may be like following.
- (NSInteger)encodeTagForButtonID:(NSString *)buttonID;
- (NSString *)decodeButtonIDFromEncodedTag:(NSInteger)tag;
When I create a UIButton, I encode the ID of the button to tag. buttonID may be something meaningful, or it is just an integer, like defined values. The signature of action can be this form: -(void)buttonAction:(id)sender;, and I can retrieve the tag value from sender.
Edit:
For example, in the UITableView's data source method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
/// new an autoreleased cell object
}
// Configure cell
cell.tag = [self encodeTagWithIndexPath:indexPath];
return cell;
}
When I touch this cell, I retrive indexPath from tag.
UIButton is a subclass of UIView, so it has tag too. For example, I made a custom actionSheet, it contains a list of UIButtons. When I push down a UIButton, I need to know which button I pressed. So, I assign the row information to tag.
NSArray *buttonListInActionSheet = ....; ///< UIButton array, a button per row.
for (int idxBtn = 0; idxBtn < [buttonListInActionSheet count]; ++idxBtn) {
UIButton *btn = [buttonListInActionSheet objectAtIndex:idxBtn];
btn.tag = (100 + idxBtn);
}
When I touch the button, I can get the row information by
- (void)buttonTouched:(id)sender {
UIButton *btn = (UIButton *)sender;
NSInteger idxBtn = (btn.tag - 100);
}