如何在单击按钮时推送 TableView 并使用 NSDictionary 初始化 TableViewCell?
任何人都可以帮助我了解如何在单击按钮时推送表格视图。 我想将消息从 NSMutableArray 加载到表视图单元格,并且 NSMutableArray 加载从 URL 解析的数据。
-(IBAction)readMessages:(id)sender
{
// i want to push the tableview when clicking the button in relation with this method
// WHAT MUST I DO HERE?
}
我不想问一个新问题,而是编辑这个问题,因为问题是同一方面的。 我现在可以以编程方式创建表格视图,但无法使用从 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 将是显示为该单元格的文本区域,在新视图中打开。我现在该怎么做?请帮助我,那里有很多教程,我也看了很多,但无法解决这个问题。
Can anyone please help me about how to push a table view on clicking a button.
I want to load the messages from NSMutableArray to the table view cells and NSMutableArray is loaded with the data parsed from a URL..
-(IBAction)readMessages:(id)sender
{
// i want to push the tableview when clicking the button in relation with this method
// WHAT MUST I DO HERE?
}
Instead of asking a new question i liked to edit this one, since the matter is in the same aspect..
I now can create the tableview programatically, but i cant initialize its cells with the data i get from Json array. Here is my code:
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
And this is the output, i want to set the titles of cells as ID and their content as text:
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"
)
As i have mentioned in my code too, 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.. How can i do it now? Please Help me, there are a lot of tutorials there, i looked a lot too but couldn't break this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Q1:您不需要添加导航即可返回主页。
当您默认使用时,
它会在下一个视图中创建返回导航以推动返回。
如果尚未创建,请在下一个视图中尝试以下代码:
Q1: U no need to add a navigation to return back to ur main page.
When ever u use
by defaults it will creates back navigation in the next view to push return back.
in case it doesn't created yet, Try the following code in next view:
试试这个:
SecondView 是你的 UIViewController 子类,它包含一个 UITableView。
Try this :
SecondView is your UIViewController subclass which hold a UITableView.
如果您有导航控件并且想要通过导航推送,请尝试以下操作:
如果您没有导航控件并且只想显示下一个视图,请尝试以下操作:
如果您在同一个类中有子视图尝试以下操作:
If u have a navigation control and if u want to pushed by navigation try the following:
if u dont have a navigation control and if u want to just display the next view, try the following:
if u are having sub view in the same class try the following:
是的,你可以,
尝试在 viewDidload 中以编程方式创建 xib:
但最好使用以下方法创建
xcode 菜单中的以下路径:
File->新->新建文件-> UIViewControllerSubClass ->;下一步->下一步->创建
或简单地拖放从你的 Interface Builder 中删除一个视图
Yes U can,
try this to create xib programitically in viewDidload:
But better to create by using
following path in xcode menu:
File-> New -> NewFile -> UIViewControllerSubClass -> Next -> Next -> Create
Or simply Drag & drop an view from ur Interface Builder
Q2:你可以使用 JSONArray 初始化你的 tableView 单元格:
Q2: U can initialize ur tableView cells with JSONArray: