Is there a simple way to shift cell content to the right? Like a horizontal alignment or some sort of offset?
I'd rather not
include a blank cell.imageView.image, or
add spaces to each cell.textLabel.text.
Any ideas please. Thanks.
Create a UIView that contains subviews positioned where you want, i.e. set their frames. Then set the cell's contentView property to this parent view.
Related
I wanted to make a UITableView with text that is both right-aligned and indented as depicted in the image below:
Unfortunately, I can not do this by writing :-
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.indentationLevel = 10; // or even -10
Can this be done using UITableView's properties? If not, the only way I could think of is using [myString drawInRect:withFont:]; but I would like to go through methods based on alignment and indentation before getting into that [I have already written code for that :-) ], so other work-arounds are welcome!
Additional info: The indentation varies with accelerometer values so I can not have hard-coded Label frame positions. I've uploaded sample code at github in which I've used only the alignment and indentation info so far, so continuing to use that would make this easier.
Subclass UITableViewCell and you can position the frame of the text label however you like. In the if statement where you dequeueReusableCellWithIdentifier: where a reusable cell doesn't exist, just modify the frame and then set the label to use the adjusted frame with setFrame. The autoresizing mask should remain the same.
You could always create your own custom cells and place a UITextLabel in the cell, and make the UITextLabel's alignment right aligned.
Also set the autoresizing mask of the UITextLabel to the right so the indentation distance stays the same no matter the orientation.
How to reduce/remove the left/right hand margin in a grouped UITableView?
Is there a way to do this without defining a custom view, i.e. using a UITableViewController directly?
I'm NOT asking here about the space between cells, it the space to the left & right of cells you see.
EDIT 1: Can I clarify:
already have a custom UITableViewCell ("#interface AppointCell : UITableViewCell") in my solution
I think the area to the left and right of these custom UITableViewCell's however are not directly from the cell itself - I say this only as when I put a border around the cell (via it's layer) I can see this - so therefore it seems like the space is from the UITableView itself (not the cells)
should point out again I'm using GROUPED mode for the table view
Sure; just adjust the frame of the UITableView so it's a little wider than its superview and a little to the left (in the negative X direction, in other words) of its left boundary.
I believe you have to create a custom cell view for your table which has padding on the left and right side.
You can also make the cell itself a bit wider. Add this method to your UITableViewCell subclass.
- (void)setFrame:(CGRect)frame {
frame.origin.x -= marginOffset;
frame.size.width += 2 * marginOffset;
[super setFrame:frame];
}
I Believe the offset should be 9 points. As a side note: If you set a custom background for the cell, you may also have to set it's selectedBackground.
You can use custom cell by extending UITableViewCell. I recommend you to take a look at http://www.e-string.com/content/custom-uitableviewcells-interface-builder
How can we set Separator style at cell level. i.e. each cell will be having different separator style?
I don't think you can set the cell separator style per cell. You might try setting the table view separator to UITableViewCellSeparatorStyleNone and then draw a custom "separator" yourself when you render out each cell.
Edit: How to do it depends a lot on your current code, what type of table, whether you are using a custom table cell, what exactly you mean by "different separator style", etc.
I have not tried this, but one option I can think of off the top of my head would be to use the UITableViewCell.backgroundView property. You could add a subview with a different color that is only a few pixels high along the bottom or you could create a UIImageView that fills the backgroundView and set the image to achieve the "different" separator.
I'm looking to write a method for an iPhone app that will auto adjust a UIViews's subviews Y values depending on the available space within that view. I'll try my best to explain without getting too confusing.
I have a Container view that is housing up to 3 Subviews. With in each subview is a button that removes the sub view from the container view, when the subview is removed i would like to adjust the remaining subviews to take up whatever space that has been opened by the removed subview.
Thanks in advance for any help!
One way is to make your wrapper view a custom UIView subclass. In that subclass, maintain a separate NSMutableArray of your subviews wherein each subview's array index corresponds to its position on the screen.
With this in place, you have a couple of options. One is to overide didAddSubview:. Based on the frame of the subview being added, you can determine the subview's position and insert reference to the subview at the appropriate index in your array.
A cleaner option is to implement your own custom method like this:
- (void)insertSubview:(UIView *)subview atPosition:(NSInteger)position;
where position is an index identifying which "slot" the subview should fill. You can set the frame for the subview explicitly within this method, along with any other subviews that are impacted by the insertion. Then insert the subview into your array at the corresponding index.
Finally, override willRemoveSubview:. In it you can use indexOfObject: on your array to find the position of the subview being removed. Then simply adjust the frames of all the subviews that follow it, and remove the subview from your array.
(One other alternative is to skip the array and just use the tag property to indicate the position of each subview. I don't like this option though. Using the tag property for anything always feels like a terrible hack to me.)
Well the first thing you need to do is signal your container that a subview has been removed.
Then you can take the height value of the view that was removed, divide it by the number of remaining subviews, and then expand the height of those remaining subviews.
Then you set your first remaining subview to Y coordinate 0, and the second (if there is one), to a Y coordinate of the first subviews height value
I have a UITableViewCell with two subviews, a UILabel on the left, and a random input control on the right. The random input control on the right can vary in size, as can the length of the text, but since I can set the word wrap of the text on the left, I need to be able to adjust the size of the UILabel based on the width of the random input control. To complicate matters, the app needs to work in both portrait and landscape modes, which give the table cells different widths.
This wouldn't be difficult if I could read the width of the table cells and set the widths of its subviews appropriately, but at creation time the width of the cell is 0.
Any ideas?
Nothing easier than that: every UITableViewCell is also a UIView, which has a method designed for just that: layoutSubviews, which is called whenever the view (here: cell) needs a re-layout. This is where you lay out the content.