单击单元格文本时如何从单元格中删除文本

时间:2012-11-08 12:15:15

标签: iphone objective-c ios uitableview uiscrollview

嗨我有问题,当我通过点击加载更多单元格显示表的记录。问题是当我点击加载更多单元格然后表必须加载两个以上的记录然后加载更多文本必须前进例如第一次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

2 个答案:

答案 0 :(得分:0)

你的问题是你只是配置一个单元格,如果它是零。在您的示例中,您将希望将单元格3的内容替换为某些歌曲,即使它不是零。

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

答案 1 :(得分: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;
}