2012-11-08 136 views
0

您好我有问题,当我通过单击加载更多单元格显示表的记录。问题是,当我点击加载更多的单元格然后表必须加载两个更多的记录,然后加载更多的文本必须向前移动例如第一次2记录加载在第三单元格上的表格加载更多的细胞,当我点击这个单元格然后还有两个记录必须加载到表格中,并且此加载更多文本移动到第5个单元格。但在我的情况下,问题是当我点击加载更多的单元格在第三的位置,它也移动到第五个位置,但在第三单元格记录不加载,也加载更多的文本出现在第三单元格。好心告诉我我该如何处理这个问题这是代码如何从单元格中删除文本单击单元格文本时

#import "RootViewController.h" 
#import "XMLAppDelegate.h" 
#import "song.h" 
#import "BookDetailViewController.h" 
@implementation RootViewController 
@synthesize mylabel,mylabel1,mylabel2,spinner,cell; 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if([myarray count]>pageSize) 
    { 
     return pageSize+1; 
    } 
    return myarray.count; 

} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 75; 
} 

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

    static NSString *CellIdentifier = @"cell"; 

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


     song *asong = [appDelegate.artists objectAtIndex:indexPath.row]; 

     NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]]; 

     UIImage *image=[UIImage imageWithData:data]; 
     if(indexPath.row<pageSize) 
     {  
      UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)]; 
      imageView.image=image; 
      [cell addSubview:imageView]; 

      CGRect CellFrame1 = CGRectMake(65, 10, 220, 45); 
      mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1]; 
      mylabel1.text = asong.title; 

      mylabel1.font = [UIFont boldSystemFontOfSize:12]; 
      mylabel1.textColor = [UIColor blackColor]; 
      [cell.contentView addSubview:mylabel1]; 
      CGRect CellFrame2 = CGRectMake(65, 47, 150, 25); 
      mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2]; 
      mylabel2.text = asong.artist; 

      mylabel2.font = [UIFont systemFontOfSize:12]; 
      [cell.contentView addSubview:mylabel2]; 




      CGRect CellFrame = CGRectMake(255, 3, 60, 7); 
      mylabel = [[UILabel alloc] initWithFrame:CellFrame]; 
      mylabel.text = asong.duration; 
      mylabel.textColor = [UIColor blueColor]; 
      mylabel.font = [UIFont boldSystemFontOfSize:10]; 
      [cell.contentView addSubview:mylabel]; 
     } 
     else 
     { 
      cell.textLabel.text = @"load more"; 

      spinner = [[UIActivityIndicatorView alloc] 
         initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 


      cell.accessoryView = spinner; 

      [spinner startAnimating]; 
      [spinner performSelector:@selector(stopAnimating) withObject:nil afterDelay:1];  [spinner release]; 

     } 
    } 






    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic -- create and push a new view controller 

    if (indexPath.row==pageSize) 
    { 

     pageSize=pageSize+2; 
     [cell reloadInputViews]; 
     [tableView reloadData]; 

    } 

    /* else if(bdvController == nil) 
     bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]]; 

    song *asong = [appDelegate.artists objectAtIndex:indexPath.row]; 

    bdvController.aartist = asong; 

    [self.navigationController pushViewController:bdvController animated:YES];*/ 
} 



- (void)viewDidLoad { 
    [super viewDidLoad]; 
    pageSize =2; 
    // Uncomment the following line to add the Edit button to the navigation bar. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    myarray = [[NSMutableArray alloc] initWithObjects:@"http://api.androidhive.info/music/images/adele.png",@"http://api.androidhive.info/music/images/eminem.png",@"http://api.androidhive.info/music/images/mj.png",@"http://api.androidhive.info/music/images/rihanna.png",@"http://api.androidhive.info/music/images/arrehman.png",@"http://api.androidhive.info/music/images/alexi_murdoch.png",@"http://api.androidhive.info/music/images/dido.png",@"http://api.androidhive.info/music/images/enrique.png",@"http://api.androidhive.info/music/images/ennio.png",@"http://api.androidhive.info/music/images/backstreet_boys.png",@"http://api.androidhive.info/music/images/adele.png",@"http://api.androidhive.info/music/images/eminem.png",@"http://api.androidhive.info/music/images/mj.png",@"http://api.androidhive.info/music/images/rihanna.png",@"http://api.androidhive.info/music/images/arrehman.png",@"http://api.androidhive.info/music/images/alexi_murdoch.png",@"http://api.androidhive.info/music/images/dido.png",@"http://api.androidhive.info/music/images/enrique.png",@"http://api.androidhive.info/music/images/ennio.png",@"http://api.androidhive.info/music/images/backstreet_boys.png",@"http://api.androidhive.info/music/images/adele.png",@"http://api.androidhive.info/music/images/eminem.png",@"http://api.androidhive.info/music/images/mj.png",@"http://api.androidhive.info/music/images/rihanna.png",@"http://api.androidhive.info/music/images/arrehman.png",@"http://api.androidhive.info/music/images/alexi_murdoch.png",@"http://api.androidhive.info/music/images/dido.png",@"http://api.androidhive.info/music/images/enrique.png",@"http://api.androidhive.info/music/images/ennio.png",@"http://api.androidhive.info/music/images/backstreet_boys.png", nil]; 
    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    self.title = @"Songs"; 
} 



/* 
// Override to support editing the list 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
    } 
    if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 


/* 
// Override to support conditional editing of the list 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 


/* 
// Override to support rearranging the list 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
// Override to support conditional rearranging of the list 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

/* 
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 
*/ 
/* 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 
*/ 
/* 
- (void)viewWillDisappear:(BOOL)animated { 
} 
*/ 
/* 
- (void)viewDidDisappear:(BOOL)animated { 
} 
*/ 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [bdvController release]; 
    [appDelegate release]; 
    [super dealloc]; 
} 


@end 
+0

if(indexPath.row priyanka

+0

大声笑我不初始化细胞与常量字符串我在细胞中添加子视图,笑loll – adnan

回答

0

你的问题是,你只配置一个单元格,如果它是零。在你的例子中,你会想用一些歌曲来替换单元格3的内容,即使它不是零。

当deque返回nil时,创建一个新单元并添加子视图,但总是根据路径更新内容。

0

请在下面尝试。

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

    if(indexPath.row<pageSize) { 
     song *asong = [appDelegate.artists objectAtIndex:indexPath.row]; 

     static NSString *CellIdentifier = @"cell"; 

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

      UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)]; 
      imageView.tag = 1; 
      [cell addSubview:imageView]; 

      CGRect CellFrame1 = CGRectMake(65, 10, 220, 45); 
      mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1]; 
      mylabel1.tag = 2; 

      mylabel1.font = [UIFont boldSystemFontOfSize:12]; 
      mylabel1.textColor = [UIColor blackColor]; 
      [cell.contentView addSubview:mylabel1]; 
      CGRect CellFrame2 = CGRectMake(65, 47, 150, 25); 
      mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2]; 
      mylabel2.tag = 2; 

      mylabel2.font = [UIFont systemFontOfSize:12]; 
      [cell.contentView addSubview:mylabel2]; 

      CGRect CellFrame = CGRectMake(255, 3, 60, 7); 
      mylabel = [[UILabel alloc] initWithFrame:CellFrame]; 
      mylabel.tag = 2; 
      mylabel.textColor = [UIColor blueColor]; 
      mylabel.font = [UIFont boldSystemFontOfSize:10]; 
      [cell.contentView addSubview:mylabel]; 
     } 
     UIImageView *imageView = [cell viewWithTag:1]; 
     UILabel *mylabel1 = [cell viewWithTag:1]; 
     UILabel *mylabel2 = [cell viewWithTag:1]; 
     UILabel *mylabel = [cell viewWithTag:1]; 


     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
     dispatch_async(queue, ^{ 
      NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]]; 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       imageView.image = [UIImage imageWithData:data]; 
      }); 
     }); 


     mylabel1.text = asong.title; 
     mylabel2.text = asong.artist; 
     mylabel.text = asong.duration; 


     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     return cell; 
    } 
    static NSString *CellIdentifier = @"cellloadmore"; 

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

     cell.textLabel.text = @"load more"; 

     spinner = [[UIActivityIndicatorView alloc] 
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

     cell.accessoryView = spinner; 

     [spinner startAnimating]; 
     [spinner performSelector:@selector(stopAnimating) withObject:nil afterDelay:1];  [spinner release]; 
    } 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 
+0

问题仍然存在,当我点击加载更多的按钮图像不加载负载更多,当我垂直拖动表格图像错误的图像显示在错误的地方请好好解决这个问题 – adnan

相关问题