TableViewCell label format different between two different NSStrings - ios

I am using a UITableView with a custom UITableViewCell. The cell just has a label on it for now for testing.
The issue that I have is that when I use the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HZEventCell *eventCell = [tableView dequeueReusableCellWithIdentifier:HZ_EVENT_CELL_REUSE_IDENTIFIER];
EKEvent *event = [EKEvent eventWithEventStore:self.dataSource.eventStore];
event.title = #"Document re-write meeting";
eventCell.label.text = event.title;
NSLog(#"Event: %#", event.title);
return eventCell;
}
This causes my label to be offset when the app loads, hanging off the edge of the screen. If I pull the actual event data from my instance of the EKEventStore then the offset changes based on the event title. Some times the entire label is centered in the cell, other times the label is 50% off the left edge of the screen. If I print the contents of event.title via NSLog, there are no formatting issues that I can see.
When I use the following code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HZEventCell *eventCell = [tableView dequeueReusableCellWithIdentifier:HZ_EVENT_CELL_REUSE_IDENTIFIER];
NSString *title = event.title; //self.data[indexPath.row];
eventCell.label.text = title;
return eventCell;
}
the label displays the text without any issues. It's not something that I am doing with the events in my data source, because the above code has me creating a new event, setting the title and assiging it to the cell's label, which still gives me an offset. It looks like cell.label.origin.x is equal to -25p. Anyone ever see this or know why this is happening? My sub-class of UITableViewCell has no code other than an outlet for the label in the header.
Note that I have this issue in iOS 6, I just happened to be doing this testing using the iOS 7 SDK when I took the screenshot.

Related

Drawing string on uiview over contentview of tableviewcell draws same content again and again

Because using three labels over UITableViewCell slowed down tableview scroll performance I tried drawing directly on UIView that I dragged over the prototype cell. While this significantly improved scroll performance, this got me into another problem.
Actually I am drawing the contents of a feed. After six or seven unique rows (for 20 records), rows are duplicate. They show the same content starting from top of tableview. However When I tap on those repeated cells the content changes to what it should have been.
After researching I found six or seven is the number of rows actually visible on the screen. So this should have been display update error but I am not sure should I do to fix this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"newsCell";
NewsCell *cell = (NewsCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.row < feeds.count) {
dict = [feeds objectAtIndex:indexPath.row];
[cell setNewsHeading:[dict objectForKey:#"title"] pubDate:[dict objectForKey:#"pubDate"] newsExcerpt:[dict objectForKey:#"attributedDescription"]];
}
dict = nil;
return cell;
}
-(void)setNewsHeading:(NSString *)newsHeading pubDate:(NSString *)pubDate newsExcerpt:(NSAttributedString *)newsExcerpt
{
self.newsView.newsHeading = newsHeading;
self.newsView.pubDate = pubDate;
self.newsView.newsExcerpt = newsExcerpt;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [feeds count];
}
That sounds like it could be a caching issue - have you tried overriding the prepareForReuse method in your cell subclass to reset the cell contents back to the default values?

UITableView not scrolling, Only 11 rows displayed based on screen size

I'm trying to display an array of strings in a table format. Following method gets called only 11 times, (the screen gets filled with 11 rows):
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyTableCell = #"MyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyTableCell];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyTableCell];
}
cell.textLabel.text = self.MyArray[indexPath.row];
return cell;
}
My array has 20 strings but only 11 are getting displayed on the screen(I'm running this on simulator). There is no scroll bar coming up for the table. The screen is stuck with just 11 rows. in my XIB i just have this tableview alone, nothing else. Can somebody help me, thanks in advance !!!!
Could you show the - tableView:numberOfRowsInSection: method. Is it returning
[self.MyArray count];
Also bare in mind that using mouse's scroll wheel doesn't work in the simulator. You have to click and drag to simulate scroll.

self.tableView reloadData is not working to vertically center my label text

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Movie *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = movie.title;
cell.subtitle.text = movie.subtitle;
cell.subtitle.numberOfLines = 0;
[cell.subtitle sizeToFit];
return cell;
}
I am calling reloadData from two places. One is from the end of a loadInitialData function, which is called from viewDidLoad.
A second one is being called from viewDidAppear, although this is inconsequential to my problem, because it existed before it and exists without it.
I initially load 3 rows of sample data, with titles and subtitles. Now what happens is my subtitle text is vertically centered when this window first launches. If I grab the table and scroll is high up, all of a sudden my [cell.subtitle sizeToFit] goes into action, and my text goes to the top vertically, which is desired.
So my issue is... why is the text vertically centered from the beginning? reloadData doesn't work either. When I return from adding a new row, all rows but the newly added row are vertically aligned to top as they should. The new row is incorrectly vertically centered.
Why doesn't this work? Everything seems good. New data is added etc. Via NSLog statements, I have verified numberOfRowsInSection is immediately called after reloadData is called.
So why does the aligning of the text vertically to the top not work?
Thanks!
This is probably because the UITableViewCell has not yet been layed out and so it does not have a size yet. Try doing the sizeToFit in this UITableViewDelegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
I am not sure this will work, but it worth trying.
Add [cell setNeedsLayout]; before you return the cell so it will layout the cell before presentation.

Dynamic sized labels inside a table not refreshing in ios

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.

iOS UIButton text not getting truncated

I am making an app that generates a UITableView from data. I have a custom UITableViewCell that contains a UIButton with text. The title of one of the generated buttons is too long for the UIButton, but isn't getting truncated as I have selected in the UIStoryBoard. The cells are getting created in my UITableViewController function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath as normal for prototype cells.
I can't add images because I'm a new user, but here are a couple links to images:
UIStoryBoard Settings: truncate setting
The title bleeding outside the boundaries of the UIButton: overflowing text
Does anyone know how to fix this. It was working earlier. I changed the size of the UIButton and the font of the title text, but I don't think that should make a difference.
Here's the code where I'm setting button's text inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CellIdentifier = #"PopoverCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
((MSPopoverCell *)cell).nameLabel.text = data.FullName; //setting the label, works fine
[((MSPopoverCell *)cell).button setTitle:data.DisplayString forState:UIControlStateNormal];//setting the button title, bleeds over
Also, I'm new to stackoverflow, so if I have missed anything that I should include, please give me some friendly advice.
Thanks,
Max

Resources