I'm trying to put some images into a UITableView, so I put an Image View into the UITableViewCell and set it aspect fit.
After starting the simulator, it appears to be right, but when I click at one cell or scroll out the originally hidden cells(may be the reused ones), the image breaks the limit of the image view's setting and destroys the layout.
the middle picture is normal, but the upper or lower one is out of shape.
cellForRowAtIndexPath function as follow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ShangbaoOriginCell";
ShangbaoOriginCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.newsSampleLabel.text = [NSString stringWithFormat:NSLocalizedString(#"Cell %d",), indexPath.row];
cell.cellView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = self.tableItems[indexPath.row];
return cell;
}
whether cell.imageView.contentMode is set or not, this problem exists.
I've added bg color, ImageView is black, Content View is blue, Cell is green.
So I saw incredible thing, the (table view) cells become not wide enough (you can see from the narrow gap between two pictures). Which is set properly in story board that the cell's width cover the whole screen.
I forgot to say that this table view is in a tabbed application, but I think this doesn't matter . And this time I have added constraints to the image view, but in vain.
Related
I have a UITableViewCell with some labels and a UIImageView that has a fixed dimension of 70x70. However, I can't seem to get rid of it when the image is nil -- a black square still appears. I have tried changing the content hugging priorities as well as changing the width to <= 70, but the empty imageview never fully disappears. Here are the imageview's current constraints:
I ended up solving it by dragging the width constraint of the UIImageView into my code and then setting its constant to 0 if the image was nil as I was configuring my cell.
you can do one thing with UIImageView when no image is there you can hide it simply. that can be implement at cell for row at index.
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.imageviewersetHidden:true];
}
or you can hide it from xib and at the time of table view cell binding you can make it visible if image available to bind. i think this may help you.
I have a TableViewController with each cell potentially having multiple lines of text (up to 3). In cellForRowAtIndexPath I am setting the cell for wrap and any number of lines. As a result, the cells successfully display wider when there are three or more lines of text (the default size allows only 2 lines of text).
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
Request *request = self.tableRequests[indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = request.question;
However, if I select a cell and go to the next view controller, when I return all the cells have reverted to a size which allows only 2 lines of text. I do not have an unwind segue; I am just using the back button as provided by the navigation controller.
When I watch closely, I believe the table actually collapses to its original cell sizes straight after selecting a cell, momentarily before the view is replaced by the subsequent view controller.
How do I make the table retain the cell size?
I have a standard UINavigationController with a UITableViewController at it's root. In IB, I paint a prototype cell with a label and a UIView. The UIView contains a button. I'd like the UIView to be x-aligned after the label, as a function of the length of text in the label.
In IB, the view's left side is initially aligned with the label's left side. There are no layout constraints in IB.
Here's my cellForRowAtIndexPath ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *labels = #[#"SOME STRING", #"SHORTER", #"A VERY MUCH LONGER ONE", #"REGULAR ONE", #"TINY"];
UILabel *label = (UILabel *)[cell viewWithTag:32];
label.text = labels[indexPath.row];
UIView *view = [cell viewWithTag:33];
CGSize size = [label.text sizeWithAttributes:#{NSFontAttributeName:label.font}];
view.frame = CGRectOffset(label.frame, size.width, 0);
return cell;
}
Two problems: I've set breakpoints and watch this code running the first time the view appears. I see the view.frame get changed for each row, but the view does not change position. I remains in it's IB-position, right on top of the label. If I scroll the table down, the views on lower cells (presumably reused) are in the desired position. If I scroll back up, the upper rows are also good. It just fails to work on the initial presentation of the upper cells.
Second problem is that the button contained in only the first row has a subtle, strange effect applied to it's text, like a blur. See attached...
First Row Button (zoomed in mac preview... see that extra blur on the left edge of the letters?)
Other Row Buttons
Stuff I tried:
I've tried a few variations, including using a regular view controller with a table view added (rather than a UITableViewController). I've found that if I reloadData on viewDidAppear, that solves the placement problem, but not the blurry button. (Also, I don't like the idea of needing to reload on viewDidAppear). Doing so on viewWillAppear has no effect at all. I've also tried animating the label change slowly. It happens, but again, only on the second time the cell is configured. I try changing the UIView color to prove the code is being run. The color change happens every time, including the first time, but not the view placement. Am I nuts?
For problem 1:
Try calling [cell layoutIfNeeded] before returning the cell, there should be no performance hit when it does not need relayout.
For problem 2:
try calling CGRectIntegral before you set the frame. ie
view.frame = CGRectIntegral( CGRectOffset(label.frame, size.width, 0) );
My table has cells which have several labels. I want one of these labels to fit its size so text begins right below the Title (remember that Labels align text vertically unless you fit its container).
Problem is, the very first time the table is loaded all labels' texts are succesfully populated but label sizes don't actually graphically apply until the NEXT time a refresh is asked. (if I ask for a reloadData with the exact same information, the labels' sizes work flawlessly).
This is some of my cellForRowAtIndexPath code:
cell.body.text = user.message;
[cell.body sizeToFit];
The only solution I've found so far is double calling [table reloadData] but this is an ugly solution. Any way I can fix this?
Edit: Previous code was a summary, I'll show the whole code here as requested:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TwitterTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TwitterTweetCell"];
// Populate cell
TweetModelData *tweet = [self.twitterModelData.tweets objectAtIndex:[indexPath item]];
cell.tweetName.text = tweet.user;
cell.tweetChannel.text = tweet.userName;
cell.tweetBody.text = tweet.message;
[cell.tweetBody sizeToFit];
return cell;
}
Regarding cell size, everything is working ok. Depending on the size of the message each cell has a different size which was pre-calculated before.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ((TweetModelData *)[self.twitterModelData.tweets objectAtIndex:[indexPath item]]).tweetHeight + 30.0f;
}
I finally found it. Don't know the reason, but it seems disabling "Use autolayout" on my storyboard fixed it.
I'm guessing auto-layout was overwriting the layout changes I was applying so they had no effect until the next data reload.
#All Hello
Its quite interesting problem i have.
Actually i have subviews which can be larger in height then cell height itself (Its required for my design). For example cell "A" height is 40 while subview height= 70.
I can show larger subview but when the cell A goes off the screen (i scroll to top) then its subview also disappear (Obvious) result. But that gives undesired effect as larger subview which before extends to cell say "B" beneath container cell suddenly disappear.
I have tried to set cell background as transparent in willDisplayCell delegate method but no luck.
Below is my related method if anyone wants to see
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSArray *newArray = [[dataDictionary allKeys] sortedArrayUsingSelector: #selector(compare:)];
[cell.contentView addSubview: [[[dataDictionary objectForKey:[newArray objectAtIndex:indexPath.section]] objectForKey:#"DayView"] objectAtIndex:indexPath.row]];
return cell;
}
Pardon me if i am not very much clear but this is best i can explain my situation.
I want to show views like http://postimg.org/image/e5ok5hybl/ image.
If you must do this, you can use a simple trick:
Move the y-origin of the table to a negative value equal to the amount your view extends below the cell, e.g. origin -30px.
Make the table height taller by the same amount, i.e. make it 30px taller.
Increase the top content inset of the table by the same amount, i.e. increase by 30px.
What you've effectively done is extend the table view off-screen while maintaining the original position of the cells. Since the table view extends off-screen, the cell will not be dequeued until the part that extends below the cell has scrolled off screen.
Here is a sample project demonstrating the trick.