i have created a controller class named TAddAlarmController which has a tableview which consists of 6 rows .When i click on the second row it navigates to a page which is a new controller name TAlarmNewController which is a tableviewcontroller and in which i have created a nsmutablearray and populated that array with 7 static values so when the second controller is displayed the tableview is displayed with 7 static values in it.I want that when i click on any row of second controller the value that is present inside the cell of the paticular row should be set to detailtextlabel of the previouscontroller i.e TAddAlarmController .Please help me in solving this problem.this is my code:
this is AddAlarmcontroller.h
#import <UIKit/UIKit.h>
@class StopSnoozeAppDelegate;
@class Alarm;
@class TAlarmNewController;
@interface TAddAlarmController : UITableViewController {
StopSnoozeAppDelegate *app;
IBOutlet UITableView *tblView;
NSDateFormatter *dateFormatter;
NSUndoManager *undoManager;
Alarm *am;
TAlarmNewController *anew;
}
@property(nonatomic,retain)NSDateFormatter *dateFormatter;
@property (nonatomic,retain)Alarm *am;
@property (nonatomic,retain)NSUndoManager *undoManager;
@end
this is my .m file
#import "TAddAlarmController.h"
#import "Alarm.h"
#import "TAlarmNewController.h"
@implementation TAddAlarmController
@synthesize dateFormatter;
@synthesize am;
@synthesize undoManager;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidUnload {
// Release any properties that are loaded in viewDidLoad or can be recreated lazily.
self.dateFormatter = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
/*
Dequeue or create and then configure a table cell for each attribute of the book.
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
//cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Time";
break;
case 1:
cell.textLabel.text = @"Repeat";
break;
case 2:
cell.textLabel.text = @"Sound";
break;
case 3:
cell.textLabel.text = @"Snooze Interval";
break;
case 4:
cell.textLabel.text = @"Alarm Message";
break;
case 5:
cell.textLabel.text = @"Snooze Penalty";
break;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TAlarmNewController *controller = [[TAlarmNewController alloc]initWithNibName:@"TAlarmNewController" bundle:nil];
switch (indexPath.row) {
case 0:
controller.editedObject = @"Time";
break;
case 1:
[self.navigationController pushViewController:controller animated:YES];
[controller release];
default:
break;
}
}
- (NSDateFormatter *)dateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
- (void)dealloc {
[super dealloc];
}
@end
this is TAlarmNewController.h
@class TAddAlarmController;
@interface TAlarmNewController : UITableViewController {
IBOutlet UITableView *tblView;
UIDatePicker *datePicker;
id editedObject;
TAddAlarmController *Addalarm;
NSMutableArray *days;//this is the array where i am storing 7 values statically
}
@property (nonatomic,retain) IBOutlet UITableView *tblView;
@property(nonatomic,retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) id editedObject;
@property(nonatomic,retain)NSMutableArray *days;
@property (nonatomic, retain, readonly) TAddAlarmController *Addalarm;
-(IBAction)cancel;
-(IBAction)save;
@end
this is my .m file
#import "TAlarmNewController.h"
#import "TAddAlarmController.h"
@implementation TAlarmNewController
@synthesize editedObject,datePicker, tblView,days,Addalarm;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
UIBarButtonItem * saveButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
days =[[NSMutableArray alloc]initWithObjects:@"Every Monday",@"Every Tuesday",@"Every Wednesday",@"Every Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday0",nil];
[super viewDidLoad];
}
- (TAddAlarmController *)Addalarm {
if (Addalarm == nil) {
Addalarm = [[TAddAlarmController alloc] initWithStyle:UITableViewStyleGrouped];
}
return Addalarm;
}
-(IBAction)save{
[self.navigationController popViewControllerAnimated:YES];
//
}
-(IBAction)cancel{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [days count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [days objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
[datePicker release];
[super dealloc];
}
@end
Q
如何通過陣列
0
A
回答
0
表示1個的tableview細胞在以往控制器的detailtextlabel文本在你FirstViewController
1,保持命名detailTextValueFromSecondController一個成員變量(的NSString)。
2,創建一個名爲
-(void)refreshTableToSetDetailText:(NSString *)detailTextValue
在SecondViewController
然後
內
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
將下面的代碼功能:
for (int i =0; i < [[self.navigationController viewControllers] count]; i++)
{
UIViewController *aController = [[self.navigationController viewControllers] objectAtIndex:i];
if ([aController isKindOfClass:[FirstViewController class]])
{
FirstViewController *aFirstViewController = (FirstViewController *)aController;
[aFirstViewController refreshTableToSetDetailText:yourstringtosetondetaillabel];
[self.navigationController popToViewController:aController animated:YES];
}
}
0
使用didSelectRowAtIndexPath
,找出用戶選擇了哪一行,並將該值存儲在NSUserDefaults
中,以獲取用於在視圖之間進行通信的特定整數鍵。
NSUserDefaults *chosenrow = [NSUserDefaults standardUserDefaults];
[chosenrow setInteger:99 forKey: StringYouWantToDisplay];
而在以前的控制器的cellForRowAtIndexPath
,拿到鑰匙的整數
NSInteger myInt = [chosenrow integerForKey:StringYouWantToDisplay];
並檢查是否爲99。如果是這種情況,那麼你可以肯定的是,該特定值被選中,併爲細胞分配detailedTextLabel
。
出頭注意:
確保
reloadData
您的tableView在ViewWillAppear
或您無法看到的detailedText
在
NSString
屬性設置所選行的值的變化的第二類。確保在前一視圖中引用該類,以便能夠從該NSString
中獲取該值。NSUserDefaults
用於在兩個視圖之間進行通信。如果選擇了一行,請檢查NSUserDefaults。如果沒有選擇一行,那麼你不需要分配一個detailedTextLabel。另一方面,如果選擇了一行,則需要分配它。
相關問題
- 1. 如何通過陣列
- 2. 通過陣列
- 3. 通過陣列
- 4. 通過陣列
- 5. 通過陣列
- 6. 通過陣列
- 7. 通過陣列
- 8. 如何通過檢查陣列
- 9. 如何發送陣列通過AJAX
- 10. 如何通過嵌套陣列
- 11. 如何通過陣列添加參數
- 12. 如何通過合併三個陣列
- 13. CouchDB的 - 如何通過陣列
- 14. 如何通過這雙陣列
- 15. 陣列通過AJAX
- 16. 通過PHP陣列
- 17. PHP通過陣列
- 18. 通過JSON陣列
- 19. 等待陣列;通過實施陣列
- 20. 選擇從陣列通過柱通過1D-陣列
- 21. 通過每次通過打印陣列
- 22. 循環通過陣列
- 23. 初學者通過陣列
- 24. 循環通過多陣列
- 25. 循環通過陣列
- 26. 爆通過,並在陣列
- 27. 排序陣列通過鍵
- 28. 是否有通過陣列
- 29. 通過從其它陣列
- 30. PHP通過多個陣列
如果僅發佈相關片段而不是整個項目,則人們更有可能閱讀代碼。 – PengOne