How to access UITableViewCell's UITextView from the UIViewController? - iphone

I have created my own UITableViewCell with XIB file. There's an UITextView inside. However, I don't know how to access its features and use its outlets with the UIViewController. What's the way to do it?
alt text http://cl.ly/a13e180c260e5e550b78/content

You can access it by pulling the subview out of the cell's subviews array and casting it to UITextView:
How you created your view hierarchy will determine the index to pass to objectAtIndex.
UITextView *textView = (UITextView*) [cell.subviews objectAtIndex:whateverIndex];

Related

iPhone: How to add a UILabel on top of a UITableView?

In my Universal IOS4 app, I have a UITableview in my xib and I control it with my UITableViewController. As you know tableview by default covers all the area in window left from the navigation bar on top and the toolbar at the bottom.
What I want is to add another UIComponent(probably a big UILabel) just under the navigation bar and place the scrollable tableview just under that UILabel, so UIlabel is not scrollable but only the table is
How can I do and control that?
Thanks
Never mind: I think this will scroll with the table.
You can set the section header to the view below. This may work if you only have one section. Best solution would be to change to a UIViewController.
Programmatically add a header to the table
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,30)] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50,30)] autorelease];
label.text = "Hello";
[view addSubview:label];
self.tableView.tableHeaderView = view;
I would suggest not to go with using UITableViewCOntroller at all. If you use UITableVIewCOntroller to ease the way you use Delegates and Data Sources, then you will have to face this kind of Customization problems.
You will have below problems if you UITableViewController:
can not set the background to a custom UIImage. You can only set the UITableView’s BG property. If you use a Custom VC and add a UITableView to it. you can entirely customize what to keep on top of the View, widtt and height of your table view.
You can not have a static Header view in your View at all. Because if you use UITableVC, you can only have the default header that you can create using Table View's data source methods. But if you use a customized VC and add a TableView to it, you can add your own customized header or controls as a header.
only advantage you will have if you use a UITableViewController is that if you have UITextFields in your Table View, UITableViewCOntroller will automatically scroll the hidden text field to above the Keypad if you start editing one.
I would not suggest you to add anything to UIWIndow, as only the first added view to window will get the rotation events
http://developer.apple.com/library/ios/#qa/qa1688/_index.html
One simple idea is to have another UIViewController (let's call this a_viewController) and set the tableview (let's call it a_tableView) and the label (samewise, a_label) as its subviews.
[a_viewController.view addSubview:a_tableView]
[a_viewController.view addSubview:a_label];
TableView header may not work in case you want the label to stay as you scroll.
But if you have only one section in your tableview, section header may become a handy option.
Can i make a table's tableHeaderView position fixed as I scroll?
One of the way is you can add on UIWindow. But make sure you handle it properly while navigating views... Following is the way...
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];
Create your UILabel instance (myView) and add it as subview in UIWindow...

Get .xib height to use it in the heightForRowAtIndexPath function

I have created a custom .xib that contains a TableViewCell that contains a UIImageView And UILabel.
I want to know TableViewCell height in the .xib file, not to set it manually in heightForRowAtIndexPath function.
Also is there a property of UIImage to have the height of the TableViewCell when i change it by hand in the .xib file?
UITableViewCell is actually a UIView, so when you load it from xib the frame property can help you:
cell.frame.size.height

Setting UIPickerView selection to UITextField inside a UITableViewCell

I have a UITextField in a custom tableview cell. On tapping the textfield area I display a popover which contains a pickerview. I need to set the selection from the pickerview to the textfield's text. I show the popover from the textDidBeginEditing method. How do I pass the selection from the pickerView's didSelect method to the textfield?
By design the entire thing is in a TableViewController with the textfield and pickerview delegate methods implemented.
Thanks!
Just set the UIPickerView's delegate to be the viewcontroller that has your UITableView. That way on the UIPickerView's didSelect (implemented in the tableview's viewcontroller) you would simply use the pickerview's datasource to populate the textfield.
To get your custom cell from a tableview you simply do:
CustomTableViewCell *thisCell = (CustomTableViewCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
Then you set the textfield in that to have whatever text you want:
thisCell.textField.text = #"text";
Can you provide a little more information? What's the tableView:cellForRowAtIndexPath: look like? I'm curious where the object you're editing is available.
There are a couple approaches I can think of...
Set the tag on the textField(s) and use it to locate the specific textField being edited
Set the delegate for the pickerView to the tableCell and have it handle the textual change
I believe the best approach is to set the UITextField's inputView property to a UIPickerView. This view will be displayed instead of the keyboard.
When a UITableViewCell is scrolled beyond a table's bounds, it may be deallocated. Therefore, storing a reference to the UITextField which invoked its textFieldDidBeginEditing: delegate method could result in an EXC_BAD_ACCESS exception.
The solution is to modify the cell's model which is stored in an array by the view controller. When the cell is initialized, it should set the text field's value to the corresponding value in its model object. Finally, the cell should observe changes to its model via KVO and update the value of the text field accordingly.

Determine whether UITableViewCell is editing from "swipe" or "self.editButton"

I'm trying to determine whether a UITableViewCell subclass is in edit mode from a user's swipe (in which case I don't need to indent my subviews) or from the user pressing the "Edit" button associated with the UITableViewController. (In which case I do.)
I know it's possible from a cell's perspective, since the self.textLabel view automatically indents properly. I have tried:
-(void)layoutSubviews {
[super layoutSubviews];
CGRect labelFrame = self.textLabel.frame;
labelFrame.origin.x += 5;
myCustomUILabel.frame = labelFrame;
}
But my custom label does not properly indent. (Though the self.textLabel view does?)
I would like to avoid the following:
Providing the cells with a reference to the parent table.
Overriding methods in the UITableViewController class to let the cells know whether they are being edited individually or the entire table is editing.
You can override willTransitionToState: in your UITableViewCell subclass. When the "Edit" button is pressed the state will be UITableViewCellStateShowingEditControlMask(=1) and when swiping it will be UITableViewCellStateShowingDeleteConfirmationMask(=2).
You should not be doing the indentation manually. The UITableViewCell will do it for you!
All you have to do is make sure that you add your subviews to 'contentView' of the UITableViewCell. This is the reason why self.textLabel indents properly as you have identified.
Look at the documentation of contentView property for a UITableViewCell:
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

Is it possible to have other UIControls on a UITableView?

I have a UITableView with 3 sections inside of a UIViewController. Is it possible to have other controls above the UITableView for example a UISlider or a UIImage? If so, how can this be accomplished?
Note: These controls should not be in a UITableViewCell, they should be part of the view.
Yes, you can add a UIView to the table header or footer, see the tableHeaderView and tableFooterView properties. A UIView could be a UIControl or a view that contains UIControls.