Update values for a UILabel placed in a UITableViews Section Header - iphone

I would like for my section header to display the sum of the values in my rows.
I therefore implement the:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
delegate function and place a UILabel in the custom view I build here.
If this were a UITableViewCell I would build the cell if it did not exist, then update it, or if it exists - only update it.
I don't know which "pattern" to use to update my Section Header.
Either there is a "right way" build into the UITableView, but I can't seem to find an "updateSectionHeaderForSection" and call this only when I change the value of a row.
What puzzles me is how the UITableView deals with headers, does it call viewForHeadersInSection only once on reloadData/instantiation or does it call it all the time, i.e. does it instantiate the view repeatedly when scrolling if I place this code in the viewController?:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"topbar.png"]];
}
And how do I force the section header to update the view, like calling updateRowsAtIndexPaths, when I have changed the value it should display?
I can't seem to find Apples take on this in the UITableView Programming Guide.
Thanks in advance for any help given.

I had to do a similar thing recently, the way I did it was to draw a custom view in viewForHeaderInSection. This view contained a number of different components and for the label that I wanted to show the total in I gave it a tag
detailLabel.tag=100001;
Then, whenever I wanted to update the total I retrieve the associated view
UILabel *total = (UILabel*)[self.view viewWithTag:100001];
And update it to the value I wanted:
total.text = #"1234";

Related

Special TableView in Iphone SDK

As you see above, I have a Table View on LeftSide which contains a Some Text.
But when i selected any annotation in map according to that the cell will be Highlighted & it has more text rather than other Cell and also background color is changed.
How can i achieve this?
You can approach in following way.
First both table data and annotation pins in Map are filled from an array!!
What you can do, you can define "tag" as array index to particular item.
When a user tap on a annotation, that annotation has a "tag" (or array index) and this "tag" (or array index) also has an item for table data.
On click of annotation tap, you have to reload your table and make that particular tableview cell highlighted.
You need to customize the UITableViewCell for this
Add a table view and map view in your view as shown in figure.
Load the custom table view cell to tableview
Initially give the needed color for all table cells
When user selects a cell change it's color
Using didSelectRowAtIndexPath: delegate method show the corresponding value on map.
Check this link for tutorials:
Appcoda
The basic sample for slide menu in iOS
https://github.com/nverinaud/NVSlideMenuController
Hope this sample code will helps you
I think you are creating custom annotation instead of the default one so if it is possible for you to set tag for each annotation view which will be same as tableview cell index. While selecting an annotation create indexpath with that tag and set selectedrow for that indexpath. Hope it may help you.
Create your customized class of UITableViewCell.
Understand you have two conditions A. Normal Cell B. Cell after the click.
You'll have to use 3 delegate methods of UITableView to achieve this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here you'll have to check for the condition you'll have to check which indexPath.row is selected. and according to that you'll have to change the height of your row.
return hight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Again the same thing. You'll have to check the condition and load the appropriate controls with appropriate frames.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is the important function for you.
// Here you'll have to set one counter. Which you'll use in the
// After that reload the table.
}

UITableView renames every eighth cell

I have a UITableView in my MainViewController. When a user taps a cell,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedRow = indexPath;
....
[self performSegueWithIdentifier:#"OtherViewControllerSegue" sender:self];
}
and they are taken to another UIViewController (let's call it OtherViewController). In OtherViewController, the name for the selected cell is set. When OtherViewController is dismissed, it updates the cell in MainViewController with the new name:
[[[mainvc.myTableView cellForRowAtIndexPath:mainvc.selectedRow] textLabel] setText:namecell.textField.text];
[self.navigationController popViewControllerAnimated:YES];
This all works fine until I have more cells than will fit on the screen. If there are more cells than will fit on the screen (8 for iPhone or 16 for iPad), then this will also set the name for every eighth or sixteenth cell respectively. Any ideas on what I am doing wrong?
Update:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [pointsTableView dequeueReusableCellWithIdentifier:#"myTableCell"];
return cell;
}
This is due to cell-reuse and you are mixing up your model with your view (in the MVC context).
A table-cell is a transient thing, once it goes off the screen it is reused (instead of creating new cells) when another cell is needed. This is what the dequeueReusableCellWithIdentifier: method does.
This means you can't store data in there and expect it to still be valid later on. In this example you are trying to store the name in the table cell. The reason to set a property (like the label text) on any view object is purely for display, not for storage. So to solve this problem you should maintain a list of objects in your model (this could be in separate classes or in an array in your mainvc object for example). Then in cellForRowAtIndexPath: you should set the label text every time - even when there should be no label you need to set it to nil or an empty string because the cells are re-used it might contain something from the last time it was used.
Update:
Instead of calling cellForRowAtIndexPath: yourself and setting its text, you should set the text in your model using a method or property in your controller and then tell the table view to reload that cell. The code might look something like this:
// This code is in where you want to set the text from
[mainvc setText:someText forIndexPath:indexPath];
.. and in your main view controller:
- (void)setText(NSString*)newText forIndexPath:(NSIndexPath*)indexPath
{
// Store the text in your model here...
...
// If the view is loaded, the table view should reload the cell.
if(self.isViewLoaded)
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
The table view will then call cellForRowAtIndexPath: where the text will be set correctly. This may seem a little convoluted at first, but when you get used to using the Model-View-Controller design pattern you will find that keeping the jobs of each MVC component separate like this will mean your code is tidier, easier to understand, has less bugs, is easier to update/extend, etc.
You're trying to store data (the new name) in a view (the cell's label). What's probably happening is that when you re-use cells in the data source's cellForRowAtIndexPath method, some of them are ones that have had this text set for them and it's still there.
The better idea is to make your changes in whatever array you use as cell information and then reload the table view to make the changes visible.
As I suppose, you shouldn't call cellForRowAtIndexPath by yourself. It can be called to create cell, not to change it.
You can update your table by passing needed string to the first view via delegate, for example. And on the event (user sets the name) you can update all table and set needed names to cells.
Hard to say exactly what the problem is, but one possible solution might be this:
Make sure that in your cellForRowAtIndexPath you are initializing the cells like this:
// Create the Cell
static NSString *recordCell = #"pickerTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:recordCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:recordCell];
}
I know this is primarily a memory solution, but might gelp here too.
Also, look through your code and check how you are determining which cell is renamed. You could be accidentally calling the rename on more than one cell without realizing it

How to add data in UITableview at run time?

I want to add data or cells in UITableview when we scrolled to the last cell of the UITableView. In other words, as we have seen in many iPhone apps, that when we reached to the last cell of the UITableview the more cells get added to it at runtime. I want to achieve that functionality in my project.
Means Add some more cells to the UITableview when we reached to the previously added last cell.
Please tell me the sample code or any tutorial for it.
I think that there are 2 questions here.
First, how to detect the end of tableVeiw? Second, how to add cells dynamically?
First question, I think it can be done by observing the value of content offset of scrollView or current indexPath of tableView cell. The content offset of scrollView can be observed by following method of UIScrollViewDelegate. The content offset is a property of scrollView.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat currentOffset = scrollView.contentOffset;
// Detect scrolling to bottom by this offset value.
....
}
The index of cell may be decided by the method of UITableViewDataSource.
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
Second question can be overwhelmed by the methods of UITableView.
beginUpdates
endUpdates
insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:
insertSections:withRowAnimation:
deleteSections:withRowAnimation:
I remember the sample codes in this official document will teach how to use above methods. They don't reload all cells, but careless operations will result in crash easily.
If you have data stored in an array, then you would definitely have the count of how many cells you want.
Use the below code for your help
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [array count];}
i believe this would have helped you.
Populate the tableView by using an array (as you should always do). When the delegate method cellForRowAtIndexPath ask your for the last cell, repopulate your array with new (more) data and make a [tableView reloadData].
you just want to add data in table?
Then, first you may need to add one more view, modal view. You should write some specification about what you want to add in this view.
Next you send a message to existing data array(Assume you use array)
[ExistingArrayData addObject:TemporalilyNewObject];
And you can sort the data by using NSSortDescriptor.
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:KEY_VALUE ascending:YES selector:#selector(caseInsensitiveCompare:)];
[ExistingArrayData sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
In this case, this method sorts the data by KEY_VALUE, ascending.
Finally, you shoule add this code in RootView's viewWillAppear method.
[self.tableView reloadData];
This code notifies app of change of data.
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
int i=0;
i=indexPath.row;
int count=[urTablearray count];
if (count == (++i)) {
//do ur stuff here
here add object to urTablearray
[urTablearray addObject:#"vijay"];
}
}
Note: urTablearray is NSMutableArray type not NSArray

How to hide this part of uitableview

I designed a uitable view like in the image below , I want to hide the part under the second section , which I write on it (<- I want to remove this part ->)
any suggestion to do that
If you coppied the code from someone, this element is the fotter view for the second section. Look for the method:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
end erase it. It should be out.
You can try creating the table with specific height in the Interface Builder or:
UITableView* pTable = [[UITableView alloc] initWithFrame:(CGRect)];
Maybe you can also specify that scrolling is forbidden in order to show the contents that you want:
pTable.scrollEnabled = NO;
You might want to implement tableView:heightForFooterInSection: and tableView:heightForRowAtIndexPath: and return heights for the footer of the current section and header of the next section to reduce the gap between the two sections. This of course, if there are sections are the current section. If there are no more sections, then you can just reduce the frame size.

UITableView change first upper/lower unused cell

Is it possible to modify the first "cell" under/over the "real" content?
Imagine this as the content or structure:
#0:[]
#1:[]
#2:[]
#3:[]
#4:[]
In this case, I would want to modify the cells with the number -1 and 5. But this should not affect the behaviour of the scrollbar. (e.g. not extending the scrollable content)
Thanks in advance
EDIT: Take a look at the facebook-refresh-cell. (Pull down the UITableView)
If I understand it correctly, you want to change the content of cells off-screen.
You actually don't want to change the cells themselves, but rather, you want to change the underlying data model that provides the cells with their data. You can, of course, do this at any time.
You shouldn't be trying to get at the UITableCell objects themselves.
Hi
Instead of trying to modify the cells, just create your own Footer and Header cells and put everything you need there, it will always stay there without modifying the actual data cells
Look into
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
}
you can do the same for Footer view just change the viewForHeaderInSection to viewForFooterInSection