how to maintain button image state in tableview - iphone

I have one table view and each cell has one button after pressing button, button image got change but if I scroll tableview then button image comes in previous state how to avoid this problem.

Store in an Array the state of the button, and when the cell is being loaded, check the state for that cell in the array and set the button properly.

Actually its reloading problem.So my suggestion is
In cellForRowAtIndexPath
if(cell == nil){
.................
alloc your button and give him a tag
[cell.contentview addSubview yourbutton];
.................
}
yourbutton = (uibutton*)[cell.contentview viewwithtag:buttontag];
if(indexPath.row == your expected index && buttonchangedBolean == YES){
yourbutton.backgroundimage = [uiimage imagenamed:#"imagename.png"];
}
If you do not understand then knock me without any hesitate it is only a suggestion to you. I can give you a solution if you post the code.Whatever try my current suggestion may it will help you.

Related

Loop through tableview Cells to see if UIImageView on cell is .hidden or not

I have a uitableview and i have a custom cell with an imageView on it. The imageView is already populated with an image, but its .hidden property is set to YES. which means when the tableView is present, all of the cells images are hiding. In the app, you may set the images on/off. This is just a little information. My problem is that i need to loop through the tableViewCells and see if there are any images hidden or not.
In cellForRowAtIndexPath: I try to loop through the cells.
BOOL cellImageViewsAreHidden = YES;
for (UITableViewCell *myCell in tableView.visibleCells) {
if (self.cell.alert.hidden == NO) {
cellImageViewsAreHidden = NO;
break;
}
else{
cellImageViewsAreHidden = YES;
}
}
if (cellImageViewsAreHidden == NO) {
NSLog(#"They are visible");
}
else{
NSLog(#"they are hidden");
}
But this unfortunately only NSLogs "they are hidden". It won't NSLog the correct text, even when there is an image visible. Any help, or suggestions are very appreciated!
Actually check the cell in the loop and not only the cell that is a property of your class by replacing
if (self.cell.alert.hidden == NO) {
with
if (myCell.alert.hidden == NO) {
Anyway, the correct way would be to store the state of the imageView in your data model. You'll need it there anyway, because you should reuse cells
Why dont you create a custom object for tht table cell and keep a status for it.
Which means create a custom object with the details of tht table cell, create a bool value to hold the status whether the images apeared or not,
Assume user have off images on app. then ste tht property to false.
If not true.
thn u dont have to play with the tableview properties. u will have more control.

How to set UITableViewCell highlighted image

I want to launch a method every time when user just highlights a cell (doesn't select it) in UITableView. Could you tell me please, how is it possible to do this ?
I want to do it because I have a custom cell with a pictures on it and I want to change pictures every time when user highlights the cell.
UPD: By highlight I mean that user just highlights a cell and don't release the finger from it. By select I mean when didSelectRowAtIndexPath is being launched (so the user releases the finger from the cell after he presses on it)
How do you envisage the user would 'highlight' a cell rather than 'selecting' one?
In iOS (or any touch based environment really) there is no concept of just highlighting a cell rather than selecting one. The only callback you get when the user touches a cell is didSelectRowAtIndexPath:
It might be worth reading up on the documentation on tables here.
UPDATE:
Ah OK, in that case you want to set the highlightedImage property of the cells imageView a bit like this;
cell.imageView.image = [UIImage imageNamed:#"normal_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"highlighted_image.png"];
I didnt understand your question..you dont you want to use the method selectRowAtIndexPath?
if you want to execute a method when user select a row:
-you can use the method selectRowAtIndexPath and execute your method.
you can also create an invisible UIButton inside cell and when click in a cell you will click in button and execute your method . . .
From iOS 6.0, UITableViewDelegate has 3 methods to handle cell highlighting:
- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:
You should use them, like in this example:
- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
MyTableViewCell* cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.myImageView.image = [UIImage imageNamed:#"myCellHigh"];
}
- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
MyTableViewCell* cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.myImageView.image = [UIImage imageNamed:#"myCellUnhigh"];
}

how to set hidden property to NO for particular cell in tableView

I am new to iphone.I have a small doubt that is,I have a table view with 66 rows initially i placed a progress view to all rows but in viewdidload i set it to hidden for all those like below in tableViewClass(ShowProgressViewCont)
cell.progressView.hidden = YES;
here (cell) is the the reference of the CustomCell class.In this class i declare a progress view and setter and getter properties also be set here in this class
here in tableview there is a download button in each cell.If we click on that download button(for example in 66th cell).we have to remove the hidden property to the progress view for that particular 66th cell only.The remaining 65 cells should have the progress view in hidden only like that for all cells.
If any body know this please help me....
Are you familiar with the concept of table cells being reused?
viewDidLoad is not an appropriate place to manipulate the content of a single cell. It may work fine if the table is so small that all of its cells fit on the screen (in both orientations).
When there are more cells in the table than beeing displayed on the screen, then those cell that became invisible recently is being reused and displayed again.
So if there are 6 cells on the screen at a time then table cell no. 7 (sometimes 8) will be identical with cell no. 1.
cellForRowAtIndexPath would be a better place to hide/unhide certain views of a cell.
If it is a custom cell already then the cell's layoutSubViews could be appropriate, too. However, only in the tableViewController's cellForRowAtIndexPath you will have easy access to both, the table's data and the associated cell.
cellForRowAtIndexPath is called every time when a cell is about to become visible. Use the default reusing-mechanism to ensure that a proper cell object will be reused. It is pretty much straight forward.
Get the cell at index 65 and then cast it to your custom cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:64 inSection:0]];
YourCustomCell *customCell = (YourCustomCell *)cell;
customCell.progressView.hidden = NO;
First set the row number in your download button in CellForRowAtInedexPath
downloadButton.tag = indexPath.row
[downloadButton addTarget:self action:#selector(actionDownload:) forControlEvents:UIControlEventTouchUpInside];
in actionDownload, make the IndexPath from button.tag and get the cell from "cellForRowAtIndexPath:",
finally update via reloadRowsAtIndexPaths: withRowAnimation:
Hide your progress view in your custom cell means make the default property of every progress view is hidden ,and then your button click method .
CutomeCell *cell = (CutomeCell *)[[button superview] superview];
[cell.progressView setHidden:NO];
NSIndexPath *rowToReload = [[self tableView] indexPathForCell:cell];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
may this help you ...
your model should be tracking the progress for every button clicked. for purposes of this answer, let's say the model is held in a property called modelInformationArray (which would contain an array of objects that relate to each cell in the table)
when the download button is clicked for a cell, modelInformationArray is modified in such a way that its object knows a download is being processed for it.
that object reports the downloading processing in a member called downloadingProcessingStarted. there are many other ways to do that part.
to get to the answer to your question … to then unhide the progress view, you would do something as follows in your UITableViewDataSource implementation of tableView:cellForRowAtIndexPath: .
- (UITableViewCell*)tableView:tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
YourCustomCell* cell = ...
if ([[self.modelInformationArray objectAtIndex:indexPath.row] downloadingProcessingStarted])
cell.progressView.hidden = NO;
}

UITableViewDataSource Method -cellForRowAtIndexPath

If I have my data in an NSArray, and I can NSLog that data in my -cellForRowAtIndexPath method, would there be any reason why I wouldn't be able see the data in my UITableView using code like:
static NSString *simpleIdentifier = #"SimpleIdentifier";
UITableViewCell *simpleCell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (simpleCell == nil) {
simpleCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}
NSUInteger row;
row = [indexPath row];
simpleCell.textLabel.textColor = [UIColor whiteColor];
simpleCell.selectionStyle = UITableViewCellSelectionStyleNone;
simpleCell.textLabel.text = [_categoriesArray objectAtIndex:row];
return simpleCell;
I'm wondering if I missed a "gotcha" or something. I have this data verbatim in one popover and it works fine. But then when I put it in another popover, it shows nothing. So I'm stumped! Any help would be great. Thanks.
How many rows and sections do you have defined for your table? If they are not defined correctly you won't have any data populating. I'm also assuming that your tableViewCell is not white, since you are setting the color of the text to white.
You're constructing your cell like this:
simpleCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
By default this creates a cell with an opaque white background. Then you're setting your text color to white:
simpleCell.textLabel.textColor = [UIColor whiteColor];
Try setting a different background color for your cell:
simpleCell.backgroundColor = [UIColor grayColor];
If you really need the table to be transparent you could set the color for both the cell and the tableView itself to [UIColor clearColor], but be aware that transparency really hurts your scrolling frame rate. You're better off using opaque views wherever possible.
Thank you everyone for trying to help me troubleshoot this problem. It was not a color issue. I finally got it to work, but I'm not quite sure why. Maybe someone else can chime in as why it works now. So in my cellForRowAtIndexPath method and other datasource methods, I have code like
if (tableView == tableView1 || tableView == _categoriesTableView || tableView == tableView2) {
...
if (tableView == tableView1) {
//do stuff
}
else if (tableView == _categoriesTableView) {
// do real stuff
}
else {
// do stuff
}
When I first had it working, I had a popover from a button that would show tableView1, then you would select a row in that table, to present another popover like _categoriesTableView. So this code worked fine. However, when I would try to change the first popover to show the _categoriesTableView, leaving all other code the same, no data would show correctly. Then instead of replacing the tableView1 initializing data for the popover, I still initialized it, but just did not present it in the popover, and now everything works. Not sure why the uninitialized memory of tableView1 would cause the problem since when I followed the code with breakpoints, my "do real stuff" was run, just wouldn't do what it was supposed to (display the data in the UITableView).

UITableView reload cells' view

How can I reload the whole tableview? In -tableView:cellForRowAtIndexPath: I'm comparing data online with data on user's device and if it's not the same then I am displaying a button to download content. After actions connected to this button I'd like to refresh the whole tableview to complare again the content online with local one.
reloadData does in fact update values but it doesn't remove these buttons. I'd like to change the way cells are displayed..
In other words I want this condition (in -tableView:cellForRowAtIndexPath:) to work and it doesnt:
if (![[NSFileManager defaultManager] fileExistsAtPath:someDir isDirectory:nil]) {
UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
(...)
[cell addSubview:bt];
}
To update the table now I have to pop the viewcontroller and push it again. I'd like to do it with button on navBar.
Thanks in advance.
That is because you are caching cells with dequeueReusableCellWithIdentifier. Check the docs. You should cache 2 types of cells, with and without the button.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: #"your-id"];
if (cell)
{
//Check if it should contain a button. If it shouldn't remove it
}
else
//Create a fresh new cell