Access to all cells in table view (Xcode, iPhone) - iphone

I use custom cells with buttons. On editing mode I need buttons to disappear.
I call this method when editing status changes:
-(void)setButtonsVisibility:(BOOL)visibility {
UIButton *currButton;
for (int i = 0; i <= [array count] - 1; i++) {
currButton = (UIButton *)[_tableView viewWithTag:10000+i];
if (currButton) {
[currButton setAlpha:visibility];
}
}
}
It works great. But this method don't work for cells that not visible now. For example, when there are more than 10 cells in table.

If you're reusing the cells properly, then the only UITableViewCells that exist in memory will be the 10 that are visible.
Easiest solution would be to just add a BOOL flag to the UITableViewDataSource instance that indicates whether to show or hide the buttons. Then the ~10 that are visible will be updated properly by your existing method, and in tableView:cellForRowAtIndexPath:, which will be called after you scroll, you can set the alpha property on the button to the flag's value.

Related

Every TableView Cell has an image, some are `hidden`, how can i test if all them are not hidden?

In my app, i have a table view and every row/cell has an imageView already on it. Each imageView already has its image assigned to it, each imageView's .hidden property is set to YES until otherwise specified by the user. My question is: How can i test to see if any images are hidden/not hidden. If 1 cell has a visible image, i want to Call a method. And if all the images are gone i want to fire a different method, Any ideas?
I was trying something along the lines of:
if ([self.hand.showBadge boolValue] == YES) {
self.handView.hidden = YES;
}
else{
self.handView.hidden = NO;
}
But unfortunately it didnt work.
Thank you!
One way could be to tag the imageView w/in the cell and then use that tag to iterate over the cells as needed.
Within tableView:cellForRowAtIndexPath, add:
myImageView.tag = MY_CELL_IMAGEVIEW_TAG;
Alternatively, you could just set the tag for your image view in a prototype cell (or your static cells), if using storyboards.
Then, if you wanted to check the visible cells, insert the following code where you'd like to check the image views.
BOOL cellImageViewsAreHidden = YES;
for (UITableViewCell *cell in self.tableView.visibleCells) {
UIImageView *cellImageView = [cell viewWithTag:MY_CELL_IMAGEVIEW_TAG];
if ([cellImageView isHidden] == NO) {
cellImageViewsAreHidden = NO;
break;
}
}
(cellImageViewsAreHidden) ? 'method for all hidden' : 'method for not all hidden' ;

How to determine if a cell contains a button

I've created a cell named CELL .
My first task is add a button on the cell, which I have successfully added.
However, while retrieving the CELL I need to know if the cell is the reusable cell or not. If not, then create the cell and add the button, but if the cell exists and button does not, I need to add the button.
In my XIB I added the button in the cell. In some cells I need to show the button and in some I don't.
How can I determine if the cell contains a button? And if it does contain a button can I tag it?
Use -
for( int i =0 ; i < [cell.subviews count]; i++) {
if ([[cell.subviews objectAtIndex:i] isKindOfClass:[UIButton Class]] ) {
//Button is found, do whatever you want
UIButton *button = [cell.subviews objectAtIndex:i];
int tag = button.tag;
}
}
One approach is to iterate though subview which I personally think is bad idea.
The other approach is to set the tag of the cell say 100 for containing button.
Then you can check
if (cell.tag == 100) {
// Cell with button
} else {
// Cell without button
}

Looping through UITableViewCells of a UITableView

So I've been trying to load cells from a UITableView and put those into an NSMutableArray to be iterated through later. I have a method called populateArrayWithCells which works fine until I put in more than 6 cells. For some reason the object returned is nil. My code is below, StocksAndAccounts is the UITableView and I have subclassed the UITableViewCells in section 1.
- (void) populateArrayWithCells
{
for (int i = 0; i < [StocksAndAccounts numberOfRowsInSection:1]; i++) {
if ([StocksAndAccounts cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]] == nil) {
NSLog(#"object at index %i is nil", i);
}
[stocksCells addObject:[StocksAndAccounts cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]]];
}
}
This function, when called through an IBAction, returns the cell successfuly until the index is 6, in which case it is nil and crashes because it's trying to add an object that is nil. Any help with this problem would be greatly appreciated.
I think you've misunderstood how UITableViews work. The cells in a tableview are only loaded when they need to be displayed, and they are recycled, so if you can only see six cells on screen at a time then the table will only ever contain six cells and it will just keep updating and reusing those same six as you scroll up and down.
Instead of storing data in your table cells, store it in an array of custom objects in your view controller and use those objects to populate the cells when the table requests them from your datasource methods.
That way if you ever need to use that data for something other than displaying in the table, you can re-create it from the original object instead of trying to copy it out of the table cell.
You are doing it backwards.
Your NSMutableArray (and preferably a sturdier model than that) should be holding what you want to display in your UITableView, not the other way around. Please review the Model-View-Controller design pattern.

How to remove everything fron the superView and not just the last item?

In my app i had to draw certain checkboxes at a same time and i used a single function to add all of them. Now when a user clicks one of them all of those checkboxes should get removed from the superview and currently its just removing the last one. Also i have issue to recognize those checkboxes like which one is clicked. i know it should be done through Tag property but don't know how exactly it should be implemented.
Any suggestions.
Removing all subviews
int numberOfSubviews = [[yourView subviews] count];
for(int i=0;i<numberOfSubviews-1;i++
{
[[youView subviews]objectAtIndex:i]removeFromSuperView];
}
//this will leave check box that you added at last.... for first one to remain loop from 1 to numberOfSubviews....
Using tag property...
when you are creating checkbox objects use
checkBoxObject.tag = i;
//I am considering i as looop count which you are using in a loop
to add checkboxes.
then whenever you need a object of checkbox
[yourViewonwhichYouAddedCheckBox viewWithTag:<your tag >];
Thanks
For identifying a "checkbox" or better said any view within an action-method:
- (void)someActionHandler:(id)sender
{
UIView *actionOriginView = (UIView *)sender;
NSLog(#"this action came from view:%d", actionOriginView.tag);
}
For assigning the tag, you may use the IB or within your code, while instantiating;
UIView *myFunkyView = [[UIView alloc] initWithFrame:CGRectZero];
myFunkyView.tag = 1337;
For removing a bunch of views from your superview - lets assume their tag is set to 10 - 15;
for (int i=10;i <= 15;i++)
{
UIView *childView = [superview viewWithTag:i];
[childView removeFromSuperview];
}

tagging a cell for identification in a selected tableview

how is it possible to tag an iphone cell in order to be able to use in a selected favorite table view. i want to be able to tagg the cell and its remaining content in order to be able to display it properly inside a new table view under a different view controller.
You set a tag with a number and by looking at UIView class ref, you set it with an NSInteger,
so a number to you and me.
tableViewCell.tag = 1
Or you extend UITableView and add your own method like
- (void) setFavorite:(BOOL)ans
{
favorited = ans;
}
- (BOOL) favorite
{
return favorited;
}
And then when you access the table view cell, cast it.
Probably better to make it a property though
#property (nonatmoic, assign, getter=isFavorited) BOOL favorited;