How to change the width of the row of a UITableView - iphone

Can we resize the width of the row of a UITableView.

What exactly are you trying to accomplish?
I'm not 100%, but I am pretty sure each row is going to be with same width as the parent table. However, if you have a custom UITableViewCell, you can make it transparent, and organize your objects on the cell's view to mimic a different sized cell. For example if you wanted a cell to be 50px less wide than the other cells and indented, you could place a white UIView on the transparent cell, make it 50px less wide than it should be, and position it to the right.
Make sense?

I understand this is an old question, but I need a table cell to be less than the width of the screen, but still be scrollable by touch on either edge, and the cell highlight not to be full width.
I overrode the UITableViewCell setFrame method, like so:
- (void) setFrame:(CGRect)frame {
if (frame.size.width > 540.0f) {
frame.origin.x = (frame.size.width-540.0f)/2.0f;
frame.size.width = 540;
}
[super setFrame:frame];
}
I guess this is a slightly hacky solution, but it works a treat!
Hope this helps someone.

Only if you fit the whole table into a smaller area.

Related

UIScrollView autoresize contentSize?

Fair warning, I'm fairly new to iOS and may be missing something obvious.
I have a UIScrollView with a few fields and those fields height can change (UITextView that stretches to fit text, collapsable fields etc). Obviously I want the scrollview contenSize to fit the content in order to allow scrolling.
Right now I have it set to re-calculate and set the contentSize every time something changes, which works fine but is there a better way to do it? In other words, if another developer now adds a field and doesn't remember to add it to the re-calculation function, unless I'm mistaken, the scrollview contentSize will be wrong even though the field is on the scrollview? Is there no way to have it "automatically" resize with it's content change?
Yes, there is a better way to accomplish this with autolayout. Apple has a very good document for using autolayout with scrollviews here. I prefer the pure autolayout approach, however in order to apply that approach you must ensure that the size of your fields are not dependent on the size of the scrollview, which makes sense. However make sure that your constraints extend to all the sides of the scroll view, so that the scrollview can determine the size of it's content.
(ie #"H:|[textView(==view)]|" and #"V:|[textView(50)]|" where '|' denotes the scrollView of which the textView is a subview and '==view' just specifies that the width of the textView must be the same as that of the view 'view'.)
If autolayout is an option for you then go with it - as Tyler stated autolayout abstracts the resizing away for you! :)
Unfortunately i have no experience yet with autolayout, so I can't tell you more about that.
Without autolayout there is no better way as far as i know.
There's nothing like "sizeToFit" or "sizeThatFits" that would take over the task of resizing the content size automatically. You have to re-calculate the content size yourself.
For that purpose either use your custom method for this re-calculation task or check out some 3rd-party categories on UIScrollView that allready have this implemented and abstracted away for you.
For example: UIScrollView+MTUIAdditions.m
(see the implementation for UIScrollView (MTUIAdditions))
The best solution
//autoresize scrollView
CGFloat scrollViewHeight = 0.0f;
CGFloat scrollViewWidth = 0.0f;
for (UIView *view in self.scrollView.subviews) {
CGFloat height = (view.frame.size.height + view.frame.origin.y);
scrollViewHeight = ((height > scrollViewHeight) ? height : scrollViewHeight);
CGFloat width = (view.frame.size.width + view.frame.origin.x);
scrollViewWidth = ((width > scrollViewWidth) ? width : scrollViewWidth);
}
[self.scrollView setContentSize:(CGSizeMake(scrollViewWidth, scrollViewHeight))];

how to reduce/remove the left/right hand margin in a grouped UITableView?

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

Transparent row of pixels between section header and first cell in a grouped UITableView

As the title says, I have a problem with a row of transparent pixels that I can't get rid off.
I attached an image as it's worth 1000 words.
The problem is not present for the section footer (where I'm also using a custom view).
I have checked and double checked that I return the correct size for the height of the section.
Any ideas?
EDIT:
I've added a test project so you can play around. The purple color represents the background color. The white rectangles are the header/footer and the light gray are some dummy cells. The issue is clearly visible in the project.
TransparentRowBug XCode project
You will also see that I use a lot of clear colors. Unfortunately changing the colors in order to hide the problem is not a viable solution for me.
Feel free to give it a try (or more)!
Thanks a lot!
EDIT2:
After further investigation, I concluded that the line of pixels come from the tableview separator. In the test project I am setting the separatorColor to clearColor. If I change this to another color, I will get exactly that row of pixels in the new color. Even though I set the separator style to none, it seems to have no effect on grouped tables.
I finally figured this one out, after almost 2 days of testing (and hair loss).
As I said in my comment above, simply returning a smaller height in the heightForHeader method doesn't work. To make it work you need to take your desired header view, insert it in a dummy/container view with the exact same size and then give this container to the tableView as the header.
Then, when you return a smaller size for the header, it magically (and it really is magic for me) manages to overlap that annoying row of pixels.
By the way, I concluded that the row of pixels is due to the cell separator line.
If someone is interested, I can upload the working test project again.
I'm not sure to understand if that's what you're asking, but I figured two days ago that you can reduce default space between sections in a tableView...
By making heightForHeader returning a negative value! Dirty, but... works.
I hope that help...
I encountered this problem too. I had a UITableView with grouped sections. Between the first section header and first cell, there was a 1-point (2 Retina pixels) gap where the background bled through.
If the background cell color is uniform, then you can assign the backgroundColor of the UITableView to hide this gap.
In my case, the backgroundColor/backgroundView was used for a non-scrolling patterned background on other cells. The solution proposed by mluisbrown works perfectly. I would like to re-emphasize this solution:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* topTabView = /* initialized elsewhere */;
CGRect sectionHeaderSize = CGRectMake(0, 0, 320, 46);
UIView* wrapperView = [[UIView alloc] initWithFrame:sectionHeaderSize];
topTabView.frame = sectionHeaderSize;
[wrapperView addSubview:topTabView];
return wrapperView;
}
Add a separator between header view and first row :- In view for Header in section delegate method add a subview self.separator //#property (nonatomic, strong) UIImageView *separator;
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return 41;
}
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
self.headerView = [[UIView alloc] init];
self.headerView.backgroundColor = [UIUtils colorForRGBColor:TIMESHEET_HEADERVIEW_COLOR];
self.separator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"seperator.png"]];
self.separator.frame = CGRectMake(0,40,self.view.frame.size.width,1);
[self.headerView addSubview:self.separator];
return self.headerView;
}
Grouped UITableView will take header, cell and footer as a single entity. So the white transparent view is the footer view which can be dealt by decreasing the footer height as shown in the screenshot. I hope it works
Good Luck :)

UILabel inside custom UITableViewCell not drawing at the correct size

I have a custom table cell which contains a number of UILabels. At runtime, I am adjusting the height of the labels to fit their contents using sizeWithFont:constrainedToSize:lineBreakMode: and repositioning them accordingly. The last label in the cell contains a large amount of text, causing it to wrap, and I'm having a very odd problem. Although the sizeWithFont call returns the correct size, and I'm setting the UILabel's frame to that height, it draws a couple of lines short. This screenshot illustrates what I'm talking about:
In this example, the height of the full block of text should be 90 (as checked in Interface Builder), and that's what returns from sizeWithFont. It's also the height that the UILabel's frame is set to, which I have verified by logging and also by stopping execution and inspecting the value. However, as you can see, it's clearly not drawing the full 90 pixels high, although it's correctly allocating the space for it (the thin black line above 'Edited' is the table cell border). I'm completely perplexed. If anyone can offer some insight as to why it's behaving this way, I would be very grateful.
At last, a solution!
Turns out that the cell does layout twice -- once during heightForRowAtIndexPath, which is where I tweak all the heights of the subviews and the cell, and later during some untraceable transaction originating in __CFRunLoopDoObservers. How did I trace this? I added a layoutSubviews override to my custom table view cell class so I could breakpoint it.
During the second pass, the last UILabel subview was getting set to a shorter height than I set it to, probably in accordance with some arcane autoresizing rules. (Yes, I tried tweaking all of those settings first, with no success.) As it turns out, merely doing nothing in layoutSubviews disabled this framework behavior, allowing me to completely control how my views draw.
With iOS 8 it doesn't work anymore like this. Implementing layoutSubviews alone doesn't do the trick, because the layout of subviews have already changed when the method is called.
I have found 2 solutions:
adding NSLayoutConstraint to layout the subviews programmatically
implementing subview's layoutSubviews and change the frame
An example für solution 2:
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect frame = self.frame;
frame.size.height = 39.f;
self.frame = frame;
}
I've fought with similar problems. It was to do with other properties being set in previous incarnations of the cell. To find it / prove it I changed the reuseidentifer for the offending cell to make sure it was a unique cell.

resize UITableViewCell textLabel

I have a UITableViewCell that I would like to add a view to the right (in addition to the accessory view). I tried setting the size of textLabel to be a few pixels narrower but it just resizes it back.
Is there any way to resize textLabel?
Actually it CAN be resized if you create an UITableViewCell subclass and override the layoutSubviews method:
- (void)layoutSubviews {
[super layoutSubviews]; //The default implementation of the layoutSubviews
CGRect textLabelFrame = self.textLabel.frame;
textLabelFrame.size.width = _textLabelMaxWidth;
self.textLabel.frame = textLabelFrame;
}
Hope it helps.
The object referenced by the default textLabel property, in UITableViewCell instances of type UITableViewCellStyleDefault, cannot be resized, at least not in my experience. The best idea in these cases is to create your own subclass of UITableViewCell, in code or even with Interface Builder if you want, and give it the layout that you want.
I've noticed that the textLabel resizes to accomodate the accessoryView. So if you want the label to use the entire cell, you could set accessoryView to nil (I think this is the default behavior anyway). Or, for example, if you want the label to take only the left half the cell, make accessoryView be an empty UIView that spans the right half the cell. (Doesn't have to be empty, you can put stuff in there...my point is that it WILL shrink the textLabel.)
You can do resizing UITableView Cell in this simplest way.First,take a look at my sample project.
ResizableUIView
It will show you how you can resize uitableview cell base on its inner component in simplest way by creating constraints.
tblTest.estimatedRowHeight = 100 // This is where you can set your estimated row height
tblTest.rowHeight = UITableViewAutomaticDimension
Another important fact is your label lines :
it should be 0
In order to make you understand,I set UILabel inside UIView,take a look at how I created constraints on its parent UIView.
If you still need any help,just ask.
Good Luck