UITableView change first upper/lower unused cell - iphone

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

Related

How to handle references to UITextFields in dynamic UITableView (iPhone)

I have a UITableView that is broken up into a user defined number of sections. Within each of these sections there are always 2 rows. Each of these two rows contains a UITextField which the user is able to edit.
What I need is a way of being able to access the data in these UITextFields at a later point. I was hoping it would be a simple problem however it's causing me a great deal of grief.
So far I have tried two approaches:
Attempt 1
I created two NSMutableArrays in which I added the UITextField objects to at index i (corresponding to the section it came from). I then tried to access the values by iterating through the array. This didn't work since the UITextFields kept getting wiped clean. Every-time I scroll down the table and the UITextField is out of view, when I go back it's contents have been wiped clean.
Attempt 2
I tried to get hold of the number of sections in the UITableView (this was fine). I then wanted to iterate through each section of the UITableView, recording the values in the rows of each. This is where I became unstuck since I'm not sure how to do this or if it's even possible.
I apologise if this is a naive question to ask, however I'm really struggling and would appreciate any advice.
Keep in mind that the text fields get reused as you scroll, so you don't really want to store references to them.
What you do instead, is to capture the information as it is entered. The easiest way to do this is to implement the textFieldDidEndEditing protocol method in the delegate.
The tricky part is figuring out which row the text field is in. The best way is to create a UITableViewCell subclass which has a NSIndexPath property. You can then set that when you configure the cell with tableview:willDisplayCell:forRowAtIndexPath:.
Then, in textFieldDidEndEditing, access the tableViewCell indexPath property through its superview. i.e.:
NSIndexPath indexPathOfParentCell = [(MyUITableViewCellSubclass *)self.superview indexPath];
Doing it this way allows you to know both the section and row of the cell.
Create your TextField in the cellForRow of the Table like so and give it a tag
UITextField * userField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, self.view.frame.size.width -20, 20)] autorelease];
userField.tag = 1001;
userField.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
userField.textAlignment = UITextAlignmentCenter;
userField.delegate = self;
userField.autocorrectionType = UITextAutocorrectionTypeNo;
userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
userField.clearButtonMode = UITextFieldViewModeWhileEditing;
if (indexPath.row == 0)
[cell.contentView addSubview:userField];
then access the TextField like so:
UITextField *userField = (UITextField *)[[(UITableViewCell *)[(UITableView *)tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] contentView] viewWithTag:1001];
Your "Attempt 1" should work OK if you keep the text field's text in your arrays rather than the text field itself. (Anything that tries to make a view or control into a data object has a good chance of going wrong.)
Whatever acts as a data source for your table view should be able to re-populate the scrolled cells according to section and row if the content is stored separately.

iphone Custom table cell, UILabel needs to be scrollable

I have a custom table cell with a UILabel in. What I want can be done with two ways.
1- Find the height of the Label that contains long text and set the rowHeight to make the whole text visible.
2- Make the UILabel scrollable so the user can move the text up/down with his hand to read the whole string.
Can you please advice me on which one to do an how to do it?
If your text is static and won't change dynamically, you should use the first option. You can cache the result so whenever you refresh the table, you won't have to recalculate the text. You can use sizeWithFont:constrainedToSize:lineBreakMode to calculate the actual text size.
For the row height: you should use tableView:heightForRowAtIndexPath: and not rowHeight, since rowHeight will change the height for ALL rows - not only a single cell.
A far as a scrollable text view inside a table: in my opinion - it looks like a cheap solution in an app and makes it a bit amateur.
I recommend not using the first option in most cases as depending on the length of the string it could make your table cells huge.
As for the second option: You should be able to add a UIScrollView as a subview into the table view cells. The Scroll View can contain the fully sized text labels and let you scroll through them within a fixed size cell.
Do we have any example on how to do it? Im new to iphone, sorry for
bugging. – Panos 9 mins ago
UIScrollView *scroller = [[UIScrollView alloc]initWithFrame:cell.frame];
scroller.contentSize = CGSizeMake(280, 100);
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
test.text = #"This would be your label.";
[scroller addSubview:test];
[cell addSubview:scroller];
This will make a UIScrollView and a label in the tablecell 'cell'. This will work, but I do not think this is the best solution, since it's gonna be a scrollview inside a tableview which already provides scrolling. My advice is to adjust the rowheigth.

UITableView - Lines Between Cells

I have a UITableView that has cells of which, for this example, let's say only the top two are filled. For the rest of the cells going down the list there are still separator lines.
Is there a way to turn the lines off where there are empty cells but keep them on to separate cells that contain information?
No there is no built in support for this.
You could turn off the cells on the table view with:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
And then draw the separator lines in your own UITableViewCell subclasses that you use for the rows.
Another simple hack is to add this to the UITableViewControllers viewDidLoad Method:
self.tableView.tableFooterView = [UIView new];
One more way is do just write the line in viewdidLoad
self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

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.

Update values for a UILabel placed in a UITableViews Section Header

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