I want to change the appearance of an UITableviewCell in edit mode like it is shown in the address book from apple. The cell should resize and i will add UITextFields as subviews.
I know that to change appearance of a cell you have to overwrite the LayoutSubviews function in the cell. I tried to do that and i had some funny effects and resizing :-)
I have looked for a while to find some hints on the net but i didnt find one.
If anyone could provide some hints how to do this right? Links to tutorials or code will be fine.
Thanks
Eddy
It's NOT a good idea to overwrite setEditing:animated: and reload your table view cells there.
That is very resource-expensive, and not the right place to do it.
In the subclass of UITableViewCell, override method didtransitionToState:
There, you can act directly on the cell outlets, like so :
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if (state == UITableViewCellStateShowingEditControlMask) {
// edit mode : peform operations on the cell outlets here
} else if (state ==UITableViewCellStateDefaultMask) {
// normal mode : back to normal
}
when you set myTable.editing=YES; it calls table view datasource and delegate method.
so if you have any data to display in table then the above code line calls the delgate method
so you can code here
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(myTable.editing==YES)
{
return 70;//customize it.
}
return 50;
}
If you want to resize the cells height you should also change "heightForRowAtIndexPath" in your UITableView Delegate.
At least that is a thing I stumbled over a few times.
Overwrite setEditing:animated: and reload your table view cells there.
Related
I am working on iPhone application . This application contains plenty of web services. I need to show list in UITableView with page by page. How to show "Loading" and call web service when scrolling down the UITableview ? Please help me..
I assume that you are adding a custom cell to the bottom of your table view to indicate the end of the current data set. When you create that cell, set its tag property to a meaningful constant you've previously defined, like END_OF_TABLE_CELL.
Then use the delegate method tableView:willDisplayCell:forRowAtIndexPath to check if the cell about to display is your custom cell, and trigger a reload of the table's datasource.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//check your cell's tag
if (cell.tag == END_OF_TABLE_CELL) {
//Call your custom method to fetch more data and reload the table
// You should also remove this custom cell from the table and add one at the bottom
}
}
This way you will have a table that refreshes itself whenever a user scrolls down far enough to bring the custom cell into view.
Good Luck!
I have custom cell created with nib. In the table view I am using the method -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath to set the height of cell
Everything works fine.
But the problem is that I also want to change the size of UIlabel which is added as subview in nib of cell.
How do I do that?
Which metod to override in customcell class ?
The method you are looking for is:
-(void)layoutSubviews
{
[super layoutSubview];
//Do your magic
}
layoutSubviews is call after the cell is created and whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations, but in this case you can also use it to redraw your subviews.
As you have UILabel in your custom cell class, make a function in that class that take frame you want to set as parameter. Set frame of label in that function. You need to call this function from your cellForRowAtIndexPath method before returning the cell.
If you are reusing your custom cell you should call method only when the (cell == nil)
Also if you can add some code in your question that would be helpful and you can get more precise answer.
You need to treat your custom cell in the same way you would treat a normal view or view controller class with a xib. i.e. You need to create IBOutlets for the controls in you custom cell and during creation of the cell you can access the controls quite easily.
myCell.myCustomUILabel.text = #"blah"
There are some gotchas when using custom cells in a xib and onnecting up the IBOutlets. This SO answer (of mine) explains how to create and link up IBOutlets of a custom cell.
After a quick search, I couldn't find answer to my problem.
The answer to this question: How to reformat a custom UITableViewCell
didn't work for me.
When my tableView gets into edit mode, the cell's subViews are indented to the right and goes off cell view. Please see attached screen shots.
I need either to reduce the indentation so that they are shifted but do not go beyond the cell. Or, when they enter edit mode, the cell's subViews are shifted a bit towards the left so that they remain within the cell.
I have designed these UITableViewCell in storyboard. I altered the indentation Level and Width in storyboard but they had no effect.
Update: Added screenshot after answer from NaCl
Normal Mode:
Edit Mode:
Change after answer from NaCl
Use this
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Simple way is in your custom cell class - in layoutSubviews method adjust your cell.view.frame
if (self.isEditing) {
// then shift cell frame to left side
}
else {
// then shift cell frame to right side
}
Your issue is likely stemming from your view hierarchy. The view's contentView automatically resizes for changes in the indentation. All your views should be subviews of contentView to resolve this issue.
I'm trying to put a button to footer of TableView.
This is what i did and I can't see the button but the string "RESULT" is displayed.
I've tried everything I can think of. but I couldn't figure out what's wrong. I need you help!
- (void)viewDidLoad {
btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSeeResult setTitle:#"RESULT" forState:UIControlStateNormal];
viewResult = [[UIView alloc] initWithFrame:btnSeeResult.frame];
[viewResult addSubview:btnSeeResult];
self.tableView.tableFooterView = btnSeeResult;
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return viewResult.bounds.size.height;
}
Anyway, what's the difference between setting the footer view directly like
tableView.tableFooterView = footerView;
and using (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section method??
There's no reason for the viewResult wrapper. A UIButton is a UIView, so there's no reason to wrap it in another UIView in this case - especially since you then assign the button to the table's footer property which means the table should be taking control of how and when the button view ultimately displays.
You may have to set the btnSeeResult's frame so that it has a height set. It's possible the height is 0 by default in that case since you don't ever set a frame for it. You might also try something like [btnSeeResult sizeToFit]; after you set the title which should resize it enough to make room for the label text, at least.
As to your second question, the difference between tableFooterView and -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section is that the former defines a view to attach to the bottom of the entire table, whereas the delegate method allows you to give each sub-section in your table different footers. Both can be used at the same time and in that case the last section's footer view would appear followed finally by the tableFooterView.
SeniorLee,
Welcome to StackOverflow!
You've got the right answer in your question - it's the latter code:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
When the table view goes to construct your table, it calls back to 2 classes - the table's delegate, and the table's data source.
Data Source is just what the name implies - the source of the data that is in your table. The Delegate, on the other hand, handles real-time interaction with the table and how it is to be displayed (e.g. what happens when the row is pressed?).
In many many cases, programmers set both the Data Source and the Delegate of the table view to the same class - the UITableViewController that owns the table view.
Anyhow, on to your question:
It's highly likely that the direct assignment isn't working explicitly because there is a delegate callback. When the table view goes to lay itself out, it will ask its delegate "do you have a view for the footer in section x?". That's the method above.
If you don't implement that method, the table view will have a default implementation that does something - in this case, probably set it to nil.
So, my guess is that even though you set this property directly in viewDidLoad, it's being overwritten to nil by the table view because you're not implementing the delegate callback. Instead, move your view code to the callback method and it should work beautifully.
Also, I don't think you need to wrap your UIButton in a UIView - UIButton is a subclass of UIView and thus should work just fine.
Is there any way to decrease the standard width of grouped UITableViewCell and put a custom button on the left side(outside of cell boundary)? I tried to change the cell size but it keeps same
You are going to have to fake the editing mode.
What I mean by that is that as AtomRiot said you have to subclass UITableViewCell so that when in editing mode you show the button you want on the left, outside the cell.
But first things first.
To change the indentation level for your cells all you need to do is implement this delegate method for the UITableView
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
So that takes care of it. Then in your UITableViewCell subclass all I would do is to implement the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
which I assume is called when the table the cell belongs to has changed to editing mode.
There I would fade in (or animate in any way you want) a button to appear on the left of your cell.
I have done it inside a grouped-style cell but never on the outside.
Give it a try!
You could subclass UITableCell and add your own custom views inside of it. I have not personally added a button inside one but it should work. It may get confused with the row selected call the tableview makes if you are implementing that.
The Cocoanetics blog seems to have a pretty good solution to this:
http://www.cocoanetics.com/2010/03/how-to-shrink-cells/