0

Found The solution with help of @parcs and @payal- This link helped -> http://www.appcoda.com/customize-table-view-cells-for-uitableview/

Basically , the table view lags when i scroll, i have gone through many post but it didn't help-

UITableView in UIScrollview scrolling delay

Delayed UIImageView Rendering in UITableView

slow scrolling of UITableView

UITableView lags while scrolling

This is the code , which i am using in cellForRowIndexpath

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *CellIdentifier = @"WorkoutCell";

        WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
            cell=(WorkoutCell *)controller.view;
        }



        [[cell contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //diplaying Workout Data

        [cell setupFont];
        cell.lblPoint.text=[NSString stringWithFormat:@"%@",[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"point"]];
        cell.lblReps.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"reps"];
        cell.lblWorkoutCategory.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"workout"];
        cell.lblWeight.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"weight"];
        cell.lblSets.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"sets"];
        //Formatting Date
         cell.lblPostedDate.text=[NSString stringWithFormat:@"Posted On: %@",[self formatDate:[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"time"]]];


        return cell;

    }

The Function SetupFont called from cellForRowIndexpath -

(void)setupFont
    {
        self.lblPoint.font=DEFAULT_FONT(13);
        self.pointLblScreen.font=DEFAULT_FONT(13);
        self.lblPostedDate.font=DEFAULT_FONT(13);
        self.lblReps.font=DEFAULT_FONT(13);
        self.lblSets.font=DEFAULT_FONT(13);
        self.lblWeight.font=DEFAULT_FONT(13);
        self.lblWorkoutCategory.font=DEFAULT_FONT(18);
        self.lblWorkoutCategory.strokeColor=kStrokeColor2;
        self.lblWorkoutCategory.strokeSize = kStrokeBigSize;
        for(UILabel *lbl in [self subviews])
        {


            if(lbl.tag==9)
            {
                lbl.font=DEFAULT_FONT(13);
            }

        }


    }

Format Date Function called from cellForRowIndexpath

#pragma mark - Format Date Function
-(NSMutableString*)formatDate:(NSString*)date
{
    NSMutableString *formattedDate=[[NSMutableString alloc]init];
    NSUInteger p = 0;
    NSUInteger length = [date length];
    while ( p < length) {
        unichar ch = [date characterAtIndex: p];
        if (ch!=' ')
        {
            if(ch=='-')
            {
                ch='/';
            }
            NSString *appendString = [NSString stringWithFormat:@"%c",ch];

            [formattedDate appendString:appendString];
            p++;
        }
        else
        {
            break;
        }
    }

    return formattedDate;

}
Community
  • 1
  • 1
  • 2
    The most unusual aspect of this code (and the probable cause of the problem) is allocating a view controller and then casting it's view as a tableviewcell. You can learn a lot by commenting out all the other config steps, maybe just leave one line like this: cell.lblPoint.text=[NSString stringWithFormat:@"%d", indexPath.row]; – danh Dec 06 '13 at 05:22
  • why you assigning view controllers to the cell ,that is making your app slow.. – Sishu Dec 06 '13 at 05:27
  • @danh- Well, i have used this process in most of the app, but till now i didn't find this kind of problem before. –  Dec 06 '13 at 05:27
  • OK, I am checking it... i will let you all know. –  Dec 06 '13 at 05:28
  • @danh and Bullet Raja-- how can i replace this code if(!cell) { UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil]; cell=(WorkoutCell *)controller.view; } –  Dec 06 '13 at 05:30
  • This looks like a decent read. The most relevant part is "Option 3" near the bottom...http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/ – danh Dec 06 '13 at 05:33
  • @danh- by Adding -> WorkoutCell *cell =(WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; and in viewDidLoad-> static NSString *CellIdentifier = @"WorkoutCell"; //register Cell [self.tblWorkout registerClass:[WorkoutCell class] forCellReuseIdentifier:CellIdentifier]; This is giving Blank cell, means cell is not appearing –  Dec 06 '13 at 05:42

3 Answers3

0

You are typecasting the UIViewController to UITableviewCell.Instead of that take UITableViewCell class add the outlets whatever you want and use it

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil] objectAtIndex:0];
    

    }

    // access that CustomCell variables here

    cell.name.text = @"Name";

}

Jyothi
  • 384
  • 1
  • 3
  • 10
  • Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.' –  Dec 06 '13 at 05:50
  • you have to subclass UITableviewCell and take an empty xib and add UItableviewcell and change the classname as your UItableviewCell class name and create outlets. that outlets you can use in your cellforRowAtIndex method – Jyothi Dec 06 '13 at 06:03
0

This is not the right way to do this:

WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
            cell=(WorkoutCell *)controller.view;
        }

Instead of this, subclass UITableViewCell and instantiate that cell here as follows:

WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil]; 
            cell = [topLevelObjects objectAtIndex:0];
        }

Also, move your setupFont method the UITableViewCell subclass.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • This is giving Blank cell, means cell is not appearing –  Dec 06 '13 at 05:48
  • Are you using xib? If yes then instantiate the cell using this: cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil] objectAtIndex:0]; – Vinay Jain Dec 06 '13 at 06:09
  • in place of this statement: cell = [[WorkoutCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; – Vinay Jain Dec 06 '13 at 06:17
  • an error mate-- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.' –  Dec 06 '13 at 06:21
  • Have you made the @property in the UITableViewCell subclass? It would be good if you can post some code. – Vinay Jain Dec 06 '13 at 06:23
  • Thanks for the code snapshot, On which statement your code breaks? – Vinay Jain Dec 06 '13 at 06:41
  • in this line ---> cell = [[[NSBundle mainBundle]loadNibNamed:@"WorkoutCell" owner:nil options:nil] objectAtIndex:0]; –  Dec 06 '13 at 06:46
  • Please use this instead and debug it and look at the array count. NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil]; cell = [topLevelObjects objectAtIndex:0]; – Vinay Jain Dec 06 '13 at 06:48
  • Your error indicates that you are setting the value to some key, that was not NSDictionary. Please share the implementation code of your WorkoutCell class. – Vinay Jain Dec 06 '13 at 06:50
  • meanwhile i am debugging as you said... u can check out the code http://pastie.org/8532649 –  Dec 06 '13 at 06:51
  • *topLevelObjects contains this ">" –  Dec 06 '13 at 06:54
  • Have you set the class of the xib to WorkoutCell? Your code looks fine. – Vinay Jain Dec 06 '13 at 07:07
0

You use

@interface WorkoutCell : UITableViewCell {}

then table view class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * const CellIdentifier = @"WorkoutCell";
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if( !cell )
{
    cell = [[WorkoutCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
}
payal
  • 162
  • 7