Медленная прокрутка в UITableviewCell - PullRequest
1 голос
/ 19 января 2012

У меня есть ячейка табличного представления в виде, в котором есть некоторый текст, стихи из библии. Но прокрутка совсем не плавная, у меня есть этот код для UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [delegate.allSelectedVerseEnglish count];


    }

    return 0;    

}



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier

                 ] autorelease];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil]; 
        cell = [nib objectAtIndex:0]; 
        cell.malayalamVerse.hidden = YES;
        cell.malayalamVerse.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;

        //pinch for serchpage
        UIPinchGestureRecognizer *longPressRecognizer = 
        [[UIPinchGestureRecognizer alloc]
         initWithTarget:self 
         action:@selector(longPressDetected:)];
        [self.view addGestureRecognizer:longPressRecognizer];
        [longPressRecognizer release];

        //longtap for simpklepopupview


        UILongPressGestureRecognizer *longPressgesture = 
        [[UILongPressGestureRecognizer alloc]
         initWithTarget:self 
         action:@selector(longPressDetectedgesture:)];
        [self.view addGestureRecognizer:longPressgesture];
        [longPressgesture release];
        if (imagedarkbackground.hidden == NO) {

            hideviewoftab.hidden =YES;
            cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
            cell.chapterAndVerse.textColor = [UIColor whiteColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.highlightedTextColor = [UIColor whiteColor];
            //cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:16];
        }
        else if (imagedarkbackground.hidden == YES){
            hideviewoftab.hidden =NO;
            cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
            cell.chapterAndVerse.textColor = [UIColor brownColor];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
        }


    }


    if(tableView == table)
    {
      UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
         myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
        [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
       cell.selectedBackgroundView = myBackView;
        [myBackView release];
        table.backgroundColor = [UIColor clearColor];
        table.separatorColor = [UIColor clearColor];
        cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
        cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:18.0];
        cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
        cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];

       // cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];

        cell.backgroundColor = [UIColor clearColor];

    }
return cell;
}

это ячейкакод высоты

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Georgia" size:18.0 ]  constrainedToSize:CGSizeMake(290.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];
    CGSize textSizelabel = [[NSString stringWithFormat:@"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0 ]  constrainedToSize:CGSizeMake(290.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];

    return textSize.height +18;
    return textSizelabel.height +18;

есть ли в моем коде ошибка, которая вызывает медленную и вялую прокрутку.Заранее спасибо.

1 Ответ

0 голосов
/ 19 января 2012

Следующий код содержит несколько исправлений:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [delegate.allSelectedVerseEnglish count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"readCell";

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil]; 
        cell = [nib objectAtIndex:0]; 
        cell.malayalamVerse.hidden = YES;
        cell.malayalamVerse.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;


        if (imagedarkbackground.hidden == NO) 
        {

            hideviewoftab.hidden =YES;
            cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
            cell.chapterAndVerse.textColor = [UIColor whiteColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.highlightedTextColor = [UIColor whiteColor];
            //cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:16];
        }
        else if (imagedarkbackground.hidden == YES){
            hideviewoftab.hidden =NO;
            cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
            cell.chapterAndVerse.textColor = [UIColor brownColor];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
        }


    }

    //** If there is only one table then move the following case also inside the cell== nil clause with few minor changes....**/
    if(tableView == table)
    {
        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
        [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
        cell.selectedBackgroundView = myBackView;
        [myBackView release];
        table.backgroundColor = [UIColor clearColor];
        table.separatorColor = [UIColor clearColor];
        cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
        cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:18.0];
        cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
        cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];

        // cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];

        cell.backgroundColor = [UIColor clearColor];

    }
    return cell;
}

Удалите следующий код в более подходящем методе, поскольку он не имеет ничего общего с ячейкой или табличным представлением. Кроме того, просмотрите свой код вif(tableView = table).Если есть только одна таблица, переместите код туда, где она подходит.

//pinch for serchpage
UIPinchGestureRecognizer *longPressRecognizer = 
[[UIPinchGestureRecognizer alloc]
 initWithTarget:self 
 action:@selector(longPressDetected:)];
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];

//longtap for simpklepopupview


UILongPressGestureRecognizer *longPressgesture = 
[[UILongPressGestureRecognizer alloc]
 initWithTarget:self 
 action:@selector(longPressDetectedgesture:)];
[self.view addGestureRecognizer:longPressgesture];
[longPressgesture release];

Я не уверен, что вы хотите сделать в методе heightForRow, так как он возвращает два значения, что невозможно.

Обновление

Если у вас два табличных представления, то вы можете использовать следующий код, поскольку оба табличных представления будут иметь отдельные ячейки, которые они будут использовать, поэтому мыне нужно устанавливать / сбрасывать другие свойства.

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

    static NSString *CellIdentifier = @"readCell";

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil]; 
        cell = [nib objectAtIndex:0]; 
        cell.malayalamVerse.hidden = YES;
        cell.malayalamVerse.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;


        if (imagedarkbackground.hidden == NO) 
        {

            hideviewoftab.hidden =YES;
            cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
            cell.chapterAndVerse.textColor = [UIColor whiteColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.highlightedTextColor = [UIColor whiteColor];
            //cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:16];
        }
        else if (imagedarkbackground.hidden == YES){
            hideviewoftab.hidden =NO;
            cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
            cell.chapterAndVerse.textColor = [UIColor brownColor];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
        }

        if(tableView == table1)
        {
            UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
            myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
            [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
            cell.selectedBackgroundView = myBackView;
            [myBackView release];
            table.backgroundColor = [UIColor clearColor];
            table.separatorColor = [UIColor clearColor];
            cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
            cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:18.0];
            cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
            cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18.0];
            cell.backgroundColor = [UIColor clearColor];
        }
        else if (tableView == table2)
        {
            // do something if needed... else leave it
        }

    }


    if(tableView == table1)
    {

        cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];

    }
    else if(tableView == table2)
    {
        // set text or changeable properties here... for table 2
    }
    return cell;
}


heightForRowAtIndexPath:

Для двух таблиц используйте следующий код (с вашими изменениями):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat height = 0.0;


    if (tableView = table1)
    {
        CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Georgia" size:18.0 ]  constrainedToSize:CGSizeMake(290.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];
        height =  textSize.height +18;
    }
    else if (tableView = table1)
    {
        CGSize textSizelabel = [[NSString stringWithFormat:@"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0 ]  constrainedToSize:CGSizeMake(290.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];
        height =  textSizelabel.height +18;
    }
    return  height;    
}

Спасибо,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...