2012-01-19 67 views
1

我在視圖中的tableview電池,其中有一些文字的bible.but滾動不是詩句都平滑我有UITableView的慢速滾動中的UITableViewCell

- (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; 

在我的代碼中是否有任何錯誤導致緩慢和緩慢的滾動。 在此先感謝。

回答

0

下面的代碼有一些修正:

- (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的方法做,因爲它返回兩個值是不可能的。

更新

如果你有兩個tableviews那麼你可以使用下面的代碼,因爲這兩個tableviews會,他們將利用,所以我們不需要設置/重置其他性質不同的細胞。

- (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;  
} 

感謝,

+0

但我有兩個的tableview細胞在一個視圖這就是爲什麼我把這種情況下,另一個tableview中是命名tab.and什麼是heightforrow問題請expalin.thanks – stackiphone

+0

這是正確的方法來設置Tableview單元格的高度根據文本大小? – stackiphone

+0

請參閱最新的答案。當你返回高度時,第二個返回將永遠不會執行,因爲函數在它遇到的第一個返回語句中完成它的執行。 – Ravin