[英]UILabel update issue for newly clicked cells in TableView - first clicked cell title is retained
我是iOS開發的新手,但經過大量搜索后仍未找到解決方法。
問題是,當單擊表格單元格時,其標題信息(從索引處的數組構建)將發送到Detail視圖控制器上的UILabel。 然后 ,單擊回到MasterViewController TableViewCells,並選擇其他單元格后,其標題不會傳遞到UILabel,並且保留了先前或單擊的第一項信息。
這可能是釋放/保留問題,或者是表reloadData問題,但是無論我嘗試在何處為我的對象或表添加這些元素,在將其發送到之前,它都不會將DetailViewController上的“ currentNodeTitle”重置為新單擊的單元格UILabel。
這是我的設置:
我有一張表格,其中包含從稱為nodeTitles的NSMutableArray創建的單元格。
數組數據的NSLog輸出為nodeTitles :(問題,“問題2”,“問題3”,“問題4”,“問題5”,“問題6”,“問題7”)
在MasterViewController.h中:
@class DetailViewController;
@interface MasterViewController : UITableViewController {
NSMutableArray * nodeTitles;
}
@property (strong, nonatomic) DetailViewController *detailViewController;
@property (nonatomic, retain) NSMutableArray *nodeTitles;
@end
在MasterViewController.m中:
從數組中,使用每個索引行的內容創建每個單元格。
nodeTitles = [[NSMutableArray alloc]initWithObjects:@"Issue", @"Issue 2",@"Issue 3", @"Issue 4", @"Issue 5", @"Issue 6", @"Issue 7", nil];
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
//*only thing added*
cell.textLabel.text = [self.nodeTitles objectAtIndex: [indexPath row]];
//*end only thing added*
return cell;}
然后對於didSelectRowAtIndexPath :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (!self.detailViewController) {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:nil] autorelease];
self.detailViewController.currentNodeTitle = [nodeTitles objectAtIndex:indexPath.row];
//[contentTitleArray release];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
}
此時一切正常。 我有一個表格視圖單元格列表,其中包含數組中的內容,單擊時將它們鏈接到UILabel。
我在MasterViewController中的Dealloc,viewDidUnload,viewWillAppear,viewDidAppear和viewWillDissappear:
- (void)dealloc
{
[self.nodeTitles release];
[_detailViewController release];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.nodeTitles = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.tableView reloadData];
}
我的DetailViewController.h:
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate> {
IBOutlet UILabel *detailDescriptionLabel;
NSMutableString *currentNodeTitle;
}
@property (strong, nonatomic) id detailItem;
@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
@property (nonatomic, retain)NSMutableString *currentNodeTitle;
@end
DetailViewController.m :
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
@implementation DetailViewController
@synthesize detailItem = _detailItem;
@synthesize detailDescriptionLabel = _detailDescriptionLabel;
@synthesize masterPopoverController = _masterPopoverController;
@synthesize currentNodeTitle;
- (void)dealloc
{
currentNodeTitle = nil;
[_detailItem release];
[_detailDescriptionLabel release];
[_masterPopoverController release];
[super dealloc];
}
在DetailViewController.m上的viewDidLoad中,我將UILabel項設置為:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = currentNodeTitle;
_detailDescriptionLabel.text = currentNodeTitle;
[self configureView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[currentNodeTitle release];
}
單擊一個項目並單擊回到主控制器后,如何獲得新選擇的單元格標題以顯示在DetailViewController的UILabel中?
謝謝〜
if (!self.detailViewController) {
self.detailViewController.currentNodeTitle = [nodeTitles objectAtIndex:indexPath.row];
}
我認為由於detailViewController有效,因此不再執行該IF語句。 將currentNodeTitle分配移出IF塊。
編輯
您將保留detailViewController,因此好像viewDidLoad僅被調用一次,並且這是您使用currentNodeTitle的唯一位置。
嘗試移動
self.title = currentNodeTitle;
_detailDescriptionLabel.text = currentNodeTitle;
to viewWillAppear:因為每次您按下detailViewController都會調用它。
制作新標簽時,只需重新加載表格即可。 一個人也應該遵循,在if(cell == nil) { [cell.contentView addSubview:label]; }
if(cell == nil) { [cell.contentView addSubview:label]; }
然后制作此標簽的標簽,然后在這種情況下查看標簽label = (UILabel*)[cell.contentView viewWithTag:labeltag]
可能會幫助您解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.