2012-01-17 16 views
0

任何人都可以請幫助我如何推單擊按鈕上的表視圖。 我想從NSMutableArray的消息加載到表視圖細胞和NSMutableArray裏裝有從URL解析的數據..如何在單擊按鈕時推送TableView並使用NSDictionary初始化TableViewCell?

-(IBAction)readMessages:(id)sender 
{ 
    // i want to push the tableview when clicking the button in relation with this method 
    // WHAT MUST I DO HERE? 

    } 

與其問了一個新問題我喜歡編輯此一個,既然事情是在相同的方面.. 我現在可以編程創建tableview,但我不能初始化它的單元格與我從Json數組中獲得的數據。這裏是我的代碼:

NSString *str1=[@"?username=" stringByAppendingString:userNameField.text]; 
     NSString *str2=[@"&password=" stringByAppendingString:passwordField.text]; 
     NSString *str3=[str1 stringByAppendingString:str2]; 
     NSString *str4 =[@"http://" stringByAppendingString:serverField.text]; 


     NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]]; 
     //get the url to jsondata 
     NSData *jSonData=[NSData dataWithContentsOfURL:url]; 

     if (jSonData!=nil) { 
      NSError *error=nil; 
      id result=[NSJSONSerialization JSONObjectWithData:jSonData options: 
         NSJSONReadingMutableContainers error:&error]; 
      if (error==nil) { 
       NSDictionary *mess=[result objectForKey:@"message"]; 
       NSDictionary *messContent=[mess valueForKeyPath:@"message"]; 
       NSDictionary *messID=[mess valueForKeyPath:@"ID"]; 
       NSString*key1=[ result objectForKey:@"key" ];     
       NSString *s1=[@"http://" stringByAppendingString:serverField.text]; 
       NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"]; 
       NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]]; 

       NSData *data2=[NSData dataWithContentsOfURL:url2]; 
       id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil]; 

       mesID = [NSMutableArray array];//saving meesage ID s to NSMutableArray 
       content = [NSMutableArray array];  
       // i logged here and it saves the data, now i want to display my data in table view 

       for (NSDictionary *data in mess) { 
        [mesID addObject:[data objectForKey:@"ID"]]; 
        [content addObject:[data objectForKey:@"message"]];   
        [[NSUserDefaults standardUserDefaults] setObject:messID forKey:@"message"]; 
        [[NSUserDefaults standardUserDefaults] setObject:messContent forKey:@"messContent"]; 

        //messID will be saved as the Title of the cells and messContent will be displayed as the text area of that cell, opening in a new view 

這是輸出,我想設置細胞作爲ID的標題及其文字內容:

2012-01-17 16:26:59.873 ipad_Teslim[940:f803] MessID: (
     1, 
     3 
    ) 
    2012-01-17 16:26:59.875 ipad_Teslim[940:f803] Content: (
     asdf, 
     "this is a test" 
    ) 

正如我在我的代碼已經提到過, messID將被保存爲單元格的標題,messContent將顯示爲該單元格的文本區域,並在新視圖中打開。我現在該怎麼做?請幫助我,那裏有很多教程,我看了很多,但無法打破這個問題。

+0

,我在我的課程的另一種方法中使用上面的代碼 – 2012-01-17 14:41:30

回答

1

Q1:U無需添加導航要返回UR主擴聲GE。 當過u使用

[self.navigationController pushViewController:next animated:YES]; 

由默認值將在下一視圖創建後退導航推回回來。

在情況下,它不尚未創建,嘗試使用以下代碼在下一視圖:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //To set the back buttin on leftside of Navigation bar 
    UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backclick:)] autorelease]; 
    self.navigationItem.leftBarButtonItem = backButton; 

} 

- (IBAction)backclick:(id)sender //first declrared in .h file 
{ 
    // To goback to the previous view 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
2

試試這個:

-(IBAction)readMessages:(id)sender { 

    SecondView *secondView =[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; 

    [self presentModalViewController:secondView animated:YES];  

    } 

SecondView是你的UIViewController子類持有一個UITableView。

1

如果妳有導航控制,如果ü要通過導航被推嘗試以下操作:

-(IBAction)readMessages:(id)sender { 
NextView *next = [[NextView alloc]initWithNibName:@"NextView" bundle:nil]; 
     [self.navigationController pushViewController:next animated:YES]; 
     [next release]; 
} 

如果u沒有一個導航控制和如果u想只是顯示下一個視圖,請嘗試以下:

-(IBAction)readMessages:(id)sender { 

    NextView *next =[[NextView alloc] initWithNibName:@"NextView" bundle:nil]; 

    [self presentModalViewController:next animated:YES]; 
      [next release]; 
    } 

如果妳有在同一個班副視點嘗試以下操作:

-(IBAction)readMessages:(id)sender { 

     [self.view addsubview nextView]; 
     } 
+0

對不起,我在Objective C中沒有經驗。我沒有任何xib文件,我想以編程方式創建視圖。我是否需要創建一個xib文件來使用這些代碼? – 2012-01-17 11:03:02

+0

請求從上午10點到下午6點(印度時間)的任何幫助.. 選擇勾選上述任何一個答案,最好是通過提高接受率來快速響應 – 2012-01-17 11:22:59

+0

謝謝@Prasad,即時在土耳其其實我不能想象你現在幾點了。我認爲它大約是晚上8點到9點:) – 2012-01-17 11:33:29

1

是U可以, 試試這個在viewDidLoad中創建廈門國際銀行programitically:

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10,300,460)]; 
    view1.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:view1]; 

不過還好用 以下在Xcode菜單路徑創建:

文件 - >新建 - > NEWFILE - > UIViewControllerSubClass - >下一步 - >下一步 - >創建

或者乾脆拖到&從烏爾Interface Builder中刪除一個視圖

+2

你可以編輯你現有的答案,而不是創建一個新的答案。 – Vin 2012-01-17 11:26:03

+1

thnks @vin,但我用這種方式,因爲ilhançetin是新用戶.. 爲他的可讀性即時通訊寫這樣。 thans再一次 – 2012-01-17 11:29:10

+0

非常感謝..對大家來說,這真的是個好地方(stackoverflow.com).. – 2012-01-17 11:31:36

1

Q2:U可以初始化UR的tableView細胞JSONArray:順便說

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [JSONarray count]; //*********** 
} 

- (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 = [JSONarray objectAtIndexIndexPath.row]; //*********** 
} 
+0

但我解析數據的一種不同的方法,當我嘗試這個在NSLog()中帶來奇怪的東西,在細胞中。我沒有嘗試過你的這種溶劑,我會讓你回來。謝謝 – 2012-01-18 10:17:00