IOS: UITableView Jerks When Scrolling Up - ios

I am having this weird issue where my UITableView starts jerking or stuttering when scrolling up, but as soon as I scroll up all the way, it is fine. I am using this table view to hold a chat conversation. I am using a custom cell, and it is only populating text on two labels.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MessagesInfo" forIndexPath:indexPath];
//
// Configure the cell...
WinMessagesInfoCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"MessagesInfo" forIndexPath:indexPath];
if (cell == nil) {
cell = [[WinMessagesInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MessagesInfo"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *userId = [defaults valueForKey:#"loggedInUserId"];
NSLog(#"Line 791 WinMessageViewController");
if ([self.sender_id objectAtIndex:indexPath.row])
{
if ([[self.sender_id objectAtIndex:indexPath.row] isEqual:userId]){
cell.friendName.textColor = [UIColor blueColor];
cell.friendName.text = #"Me";
}
else
{
cell.friendName.textColor = [UIColor greenColor];
cell.friendName.text = [self.from objectAtIndex:indexPath.row];
}
cell.friendMessage.text = [self.message objectAtIndex:indexPath.row];
}
return cell;
}
Update:
This issue appears to be the height of my cells, if I take out the dynamic cell growth, the issue goes away, but I need them to grow dynamically. I am currently using the following:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
The estimates must be bad. Any advice to get a better estimate? There are two labels in the cell and only one of the labels will change in size.

Related

Match TableViewCell's Label from other TableViewCell's Label

My problem is little complicated to define for me, even I understand what I am trying to do, I try my best to write this problem to understandable, guide me if I lack anything in question description.
So the problem is that I have a UITableView inside a UITableViewCell, both of UITableViews have custom UITableViewCell classes. Datasource and delegate of both outer an dinner UITableView's are in same class. I am handling the data array like this
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView==self.mainTableView) {
return titleArray.count;
}
return timeArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.mainTableView)
{
MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:#"cell"];
cell.label.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.view.backgroundColor = [UIColor greenColor];
return cell;
}
My custom cell's description
Main UITableViewCell cusotm class properties - UILabel (eg.
myLabel),UITableView(inside)
Inside UITableViewCell custom class properties - UIView (eg.
myView)
Now I want to show myView only on that cell when 'myLabel' matches with my array of string object. for eg.
if([myLabel.text isEqualTostring:#"22"]){
cell.myView.backgroundColor = [UIColor blackColor];
}
For eg. if I have 5 views and 2 label, now 2 rows in Main UITableView and 5 rows in inside UITableView but every view should be in the outer cell
only if it matches the myLabel'text.
P.S Don't think about where matching data coming from its not an issue issue of matching data.
As a final conclusion I want to create different row count for each inner UITableView cell.
Did you tried this?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if( tableView == self.mainTableView )
{
MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:#"cell"];
cell.label.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
MainTableViewCell *parentCell = (MainTableViewCell*)cell.superView;
NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];
NSString *text = [dataArray objectAtIndex:parentIndexPath.row];
if( [text isEqualTostring:#"22"] //your condition goes here
{
cell.view.backgroundColor = [UIColor blackColor];
}
else
{
cell.view.backgroundColor = [UIColor greenColor];
}
return cell;
}

Custom table view cells show up blank (labels are nil)

I'm using the same code that I use for other tables in my app, but I'm missing something here. None of my labels are being set as you can see in this image.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommissionsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CommissionsCell"];
CommissionInfo* tmpInfo = [g_commissionList objectAtIndex:[indexPath row]];
cell.backgroundColor=[UIColor colorWithWhite:0.95 alpha:1 ];
cell.lblDescription.text = tmpInfo.description;
cell.lblDate.text = tmpInfo.date;
cell.lblStatus.text = tmpInfo.status;
if([tmpInfo.imgThumbPath isEqualToString:#""]){
cell.lblInstructions.hidden=NO;
}else{
cell.lblInstructions.hidden=YES;
}
[cell.btnImg setImage:tmpInfo.img_thumb];
return cell;
}
I have all of my labels mapped in the CommissionsCell.h file any idea what's causing this?

UITableView textlabel frame not showing proper

I developed iPhone app in which i have UITableView.
UITableViewCell textlabel frame is not proper according to text,
when i write this code, UITableView textlabel is not proper.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:12.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [tempArr objectAtIndex:indexPath.row];
return cell;
}
when i pass my array to UITableView it shows like this:
Whats the problem ? where i am doing mistake, please help
I am not sure what text your array contains. But, I have created an example of my own. Here is the code :
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor = [UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:20.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = #"Brand";
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor redColor];
}
#end
Here is the output :
I have used your code. Only difference I made is, I am applying red colour to text label in willDisplayCell: method of UITableViewDelegate.
Coming to the margins that appears on both sides of the tableview are as per Apple's UI guidelines.
Let me know if you need my code.
Try to set Size to fit with content After setting Text.
[cell.textLabel sizeToFit];
Thanks.
Check table row height property in size inspector, may be some thing wrong with it
[tableView setRowHeight: 100.00];
or you can use delegate to set row height
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

UITableViewCell with UIScrollView cause content of other cells disappearing

I was trying to find solution almost everywhere, but I didn't find it. So, here is my problem.
I have UITableView with custom UITableViewCells.
The first cell has UIScrollView inside its Content View.
The Second cell has UILables and other basic views inside its Content View.
So, if there is UIScrollView inside the first cell, content of the second cell disappears. It appears only if the first cell scrolls out of the tableView frame.
Can anybody help me figure it out? Thank you.
Code preview
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath]) {
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else if ([indexPath isEqual:_adDetailsPath]) {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
}
}
Might be issue with cell drawing on iOS 7.1, according answer on iOS 7.1 beta5 tableviewcell height showing objects outside it's range, try to clip subviews:
cell.clipsToBounds = YES;
Try it
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath])
{
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
return cell;
}
}

iOS: uitableview redraw objects on first cell to bottom cells

i have a uitableview with custom cells.. with normal code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DDMainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[DDMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
the problem is when i select one cell i add progress bar on the cell that download data online.. but when i Scroll down i find that every 10 cells have the same progress bar .. how can i prevent this behavior ?
Try this,
- (void)viewDidLoad
{
[super viewDidLoad];
dataarr=[[NSMutableArray alloc]init];
indexarr=[[NSMutableArray alloc]init];
mytableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
mytableview.dataSource=self;
mytableview.delegate=self;
[self.view addSubview:mytableview];
for (int i=0; i<30; i++) {
[dataarr addObject:[NSString stringWithFormat:#"%d",i]];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataarr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text=[dataarr objectAtIndex:indexPath.row];
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[act setFrame:CGRectMake(50, 20, 20, 20)];
act.hidden=YES;
[cell.contentView addSubview:act];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[act startAnimating];
act.hidden=NO;
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[mytableview reloadData];
return;
}
[indexarr addObject:[dataarr objectAtIndex:indexPath.row]];
[mytableview reloadData];
}
Make sure, when downloading is complete, then remove this object from indexarr....
That's because your cells are getting reused; UITableView will put off-screen cells into the reusable cell queue, and dequeue them for reuse if the reuseIdentifier matches. You should use some other data structure (e.g. NSArray or NSDictionary) to track which indices/cells have already been tapped. Then, in the method you showed above, regardless of whether the cell was init-ed or dequeued, set the progress bar according to your underlying data structure.
Here your used UITableViewCellIdentifier is reuseIdentifier. Which will work for all Cells are same type. Now your are taking once cell with progress bar. Now it will different from all cells data.
So use one more tableview cell for progress bar, or while reloading table remove the Progress bar which is exists already and add again. for this use tag for progress bar.

Resources