UITableViewCell subview (left side) - iphone

Is it possible to implement a subview of a UITableViewCell on the left side of the cell that crossfades when the table enters the editing mode?
Another problem I face is that the bounds of the cell.textLabel are read-only. Is it possible to use some sort of inset for that label? (Because I, like mentioned above, want to use a View on the left side)
Edit: How do I perform an action (in this case fade a subview) when the whole table enters editingmode? (Not through a swipe over a cell). The reason why I want to implement this is because the tableview shows the "-" button on the left side of the cells if it is in editingmode. (I want to show my own button on the left side of the cells if editing=NO, fade it out if editing=YES and show it again if the tableView leaves editingmode (editing=NO))
A first thought directs me to something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
//fade out my own button
} else {
//show my own button
}
}
But I would have to loop through all the cells and I think this leads to a substantial performance worsening. What do you think?

You want to set the Editing Accessory View of a UITableViewCell. See the editingAccessoryView property for information on this. More information can be found in the Customizing Cells section of the TableView Programming Guide.
The label within the content view of a table view cell is read-only so you can not reassign it but its properties are mutable. You can actually move the label around within the bounds of the table view cell. It's probably confusing to grasp, but have a look at the Characteristics of Cell Objects section of the Table View Programming Guide. In it, the bounding boxes for each of the nested views are shown (not all of them are always visible). For your purposes, a good exercise would be to set the background color of each subview of a UITableViewCell to a different color and then try to adjust the sizes of them. Doing so and understanding what's going on will probably let you achieve the end-result that you desire.

In the end I created a custom UILabel that I used instead of the default UITableView textLabel and added a custom button on the left side. In the setEditing method I fade this button in/out.

Related

View with uitableview, tabbar and more objects

I'm developing an app and I need a view with these elements:
- UITableView
- UITextField and UIButton
- TabBar
As you can guess, I am developing a chat, but when I put the elements in the .xib, I can't show de layer: UITextField and UIButton. If I put them without a view, they dont appear.
If I put a view under the table, and in that view I put the text and the button I get this error: http://pastebin.com/CKfxijz9 (I put the error there because it's to long)
Thanks in advance
There are some ways of dealing with that, depending on how it should behave. Some of them are:
Provide a table footer or header view that holds the button and
the text field.
Nest the table view into another view. The table
view and the view containing the button and text fielt are on the
same level within the view hierarchy. They are rather siblings than
sub- and superview to each other.
Use a UISlider instead of a table. (However, I personally would use the table.)
Here's an approach I've used in the past (not sure if it's best practice, but it works).
Add your button and textfield to a new view (let's call it, bottomView)
Add bottomView to the superview of your tableview
Set the frame of your bottom view so that it fits to the bottom of the screen (this will make it so your tableview will scroll, but keep your bottomView always attached to the bottom of your mainview)

Custom UITableViewCell with Storyboard

My goal is to create a custom UITableViewCell which contains 2 UILabels. One is the title and one is the detail. The detail label should allow for 3 rows of text.
So I went on and created a custom table view cell in storyboard. I also created a subclass of UITableViewCell and linked the two together.
I the added two UILabel to the cell in storyboard and placed them where i wanted them to be and linked them to their coresponding outlets in teh subclass. Since the content of the labels varies I wanted to align the text vertically to the top. As I understand the only way to do this is by calling the sizeToFit method on the label. I execute this under in the sub class of UITableViewCell:
-(void)layoutSubviews {
[super layoutSubviews];
[self.detailTextLabel sizeToFit];
}
So far everything seems fine and the text in the detailTextLabel is aligned as it should. Although when i satrt interacting with the cell, for example slide my finger over it so the delete button appears, the detailTextLabel will change size to the size that was set in storyboard. This causes the text to be misaligned. Similar things happen when i select the cell and change to another view and the return to the table view via a tab bar
My question is: Is there any way of creating this custom cell differently using storyboard or is my only alterative to create everything programtically?
Regard, Christian
Maybe you should take a look at this if you still want to vertically align your text in your UILabel without sizeToFit (who will resize it when you will interact with your cell).
About your question, I think you can create your custom cell from a xib file like this.

Can I customize the check mark position in UITableView?

I'm working on a UITable with allowsMultipleSelectionDuringEditing = true, then I get a circle and red checkmark in the table when it is editing:
The problem is, I need to customize the table view cell such that there are some background image for the cell, and I need a larger margin in the left side of the cell. I can customize the cell contentView for the content position, but are there a way I can customize the checkmark and circle position?
I could have implement a customize table view cell with my own multi selection logic and view, but are there a way I can do it with the built-in multi-select implementation? If not, would you recommend a idiomatic way to do it?
it is possible to change the position of the check mark,
Please make a Table View cell XIB and give it a background image and check mark image and a label ..
the hiding and the display of the check mark will be managed by the associated class of XIB.
it will work for you.
** All the best
To answer my own question, you cannot move the red checkmark and circle (editing control) with public API.
You can however, override the UITableViewCell layoutSubviews method. Inside it find the subview which is a "UITableViewCellEditControl", and modify its position as you wish.

UITableViewCell subclass - how to add a button to some rows

I have a UITableViewCell subclass that has 3 labels and sizes and positions the labels based on the text length. When selected all rows perform the same function. I'd like some rows to also have a detail view associated with them. So I want to add a UIButton to a few rows, but I'm not sure how to do it.
Should I create a UIButton member of the UITVCell subclass and in layoutSubviews if the button is not nil I can make room for it?
What would cellForRowAtIndexPath look like? Do I need two reuse identifiers ? One for button and one for no button? Or just create the button and the cells that have buttons will automatically detect the button in layoutSubviews and make room for it?
I can't find any examples online of someone doing this without using XIB..
EDIT - So I added the detail disclosure button as suggested below, I can detect that my cell has an accessoryType, but the accessoryView width and height are garbage.. I need a width and height because right now the accessory is on Top of my row text since I'm not accounting for it in my layoutSubviews code..
// CustomTableViewCell.m:
- (void)layoutSubviews {
// I need a width / height here so I can accomodate the accessory button.
if ( self.accessoryType != UITableViewCellAccessoryNone ) {
NSLog(#"accessory! %f %f",self.accessoryView.frame.size.width, self.accessoryView.frame.size.height);
}
}
You don't need to add a UIButton to rows that have a detail view -- just allow them to have a disclosure button (note: button, not indicator!). A disclosure button, when tapped, typically shows a detail view for that cell.
To enable the disclosure button for a certain cell, do the following in the cellForRowAtIndexPath method:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
More details here:
What's the difference between an Detail Disclosure Button and an Disclosure Indicator?

Decrease UITableViewCell width and put custom button on the left

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/