UITableView reload cells' view - iphone

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

Related

Getting Data from a tableview and store to sqlite database

I Have a form with Tableview and Custom Cells with many number of text fields,Label and textViews.If a user Fills All fields and He want to save the Form then How can i get all the field values on my Button click method ?
I would highly appreciate it if someone could help me out by showing me how to do this.
// get the path of cell from row and section
-(void)btnClick
{
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
customTxtField *cellCustomTxtField = (customTxtField *)[mytableview cellForRowAtIndexPath:path];
NSString *str = cellCustomTxtField.text1.text;
}
Don't wait until the button click event to retrieve the cell contents. Because cells are reused (e.g., during scrolling), the values in each cell will not be preserved.
As cell values change, update the model that drives your table by making the tableViewController a delegate for the respective input fields.
For example, make the tableViewController the textField delegate and update the model as the user inputs data. To distinguish among textFields in the tableViewController, assign a tag to each textField when creating the cell (i.e., tableView:cellForRowAtIndexPath).

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;
}

how to maintain button image state in tableview

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.

Is it possible to accept a button tag to be in some range iphone sdk

In my application I'm doing dynamic resizing of cells and label in it depending upon the text in it.
I'm adding button to cells in uitableview.
I'm taking the label instance and button instance in a new label and button variable respectively and setting their frames to arrange them properly after resizing.
if(cel==nil)
{
//some code
original_label=[[UILabel alloc]init];
original_label.tag=111;
//SOME MORE CODE GOES HERE
original_button=[[UIButton alloc]init];
original_button.tag=222;
//SOME MORE CODE GOES HERE
}
new_label=(UILabel *) [cell viewWithTag:111]; //This' how I'm taking the label instance on cell and below button instance on cell in new variables
new_button = (UIButton * ) [cell viewWithTag:222];
Earlier I kept the tags of all the buttons on cells same, so it was easier to get button instances on cells properly and were being arranged properly. But now I want to recognize these buttons separately as I'm adding some functionality on button_click. I'm giving the buttons that are added to the cells incremental tags[1,2,3...9 and so on]. Now, how can I take these button tags in some range like[suppose 1-9]?
Can anybody help?
Thanks in advance.
You can keep the button tags the same as you had before.
Instead, in the button_click method, figure out which row the button is in like this:
- (void)button_click:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//code to handle this indexPath.section and indexPath.row...
}
This assumes you have added the button to cell.contentView which is what the first superview gets. The second superview gets the cell.
The addTarget for the button should look like this (note colon after button_click):
[original_button addTarget:self action:#selector(button_click:) forControlEvents:UIControlEventTouchUpInside];