单击单元格文本时如何从单元格中删除文本
Posted
技术标签:
【中文标题】单击单元格文本时如何从单元格中删除文本【英文标题】:how to remove text from cell when click on the text of cell 【发布时间】:2012-11-08 12:15:15 【问题描述】:嗨,当我通过单击加载更多单元格来显示表格记录时,我遇到了问题。问题是,当我单击加载更多单元格时,表格必须再加载两条记录,然后加载更多文本必须向前移动,例如第一次在第三个单元格的表格上加载 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
【问题讨论】:
if(indexPath.row你的问题是你只配置一个单元格,如果它是零。在您的示例中,您将希望将单元格 3 的内容替换为某些歌曲,即使它不是 nil。
当 deque 返回 nil 时,创建一个新单元格并添加子视图,但总是根据路径更新内容。
【讨论】:
【参考方案2】:试试下面。
- (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;
【讨论】:
问题仍然存在,当我点击加载更多按钮图像未加载更多加载时,当我垂直拖动表格然后图像错误图像显示在错误位置请解决此问题以上是关于单击单元格文本时如何从单元格中删除文本的主要内容,如果未能解决你的问题,请参考以下文章