我在Viewcontroller A上有一本字典。我在viewcontroller上传递它B.On ViewController它在viewcontroller字典上显示数据B.But,在Viewcontroller中它不显示任何数据吗?
ViewController A:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (cell== nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(_List.count>0) {
[cell setupCell:[_List objectAtIndex:indexPath.row]];
ViewControllerB *vc=[[ViewController alloc]init];
vc.userIdInfo=[_List objectAtIndex:indexPath.row];
}
return cell;
}
ViewController B:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"_userIdInfo %@",userIdInfo);
if([userIdInfo valueForKey:@"UserID"]) {
[self fetchDataFromServer:getdetail];
}
}
此NSLog(@"_userIdInfo %@",userIdInfo)
打印null
。而vc.userIdInfo
打印字典
{
Count = 3;
UserID = "6gdab241-j176-1121-lde2-as8d21f62231";
UserName = "abc@gmail.com";
}
答案 0 :(得分:0)
您需要在DID选择方法中执行ViewController B初始化,如下所示。
不要在cellForRowAtIndexPath
初始化它。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 1st Type
ViewControllerB *vc=[[ViewController alloc]init];
vc.userIdInfo=[_List objectAtIndex:indexPath.row];
[self.navigartionController pushViewController:aVC animated:YES];
//2nd Type
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewControllerB *aVC = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
aVC.userInfo = <set your data>;
[self.navigartionController pushViewController:aVC animated:YES];
}
希望这会有所帮助。
答案 1 :(得分:0)
代码
ViewControllerB *vc=[[ViewController alloc]init];
vc.userIdInfo=[_List objectAtIndex:indexPath.row];
只创建ViewController;它没有在任何地方呈现它。此外,实例 vc 将在您离开
范围后立即解除分配if(_List.count>0){}
你的方法
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"_userIdInfo %@",userIdInfo);
if([userIdInfo valueForKey:@"UserID"])
{
[self fetchDataFromServer:getdetail];
}
}
永远不会调用,因为您的视图永远不会加载。
我认为,您真正想要的是使用navigationController实例或当前viewController来呈现您的vc实例
[self.navigationController presentViewController: vc animated: YES completion: nil];
答案 2 :(得分:0)
在目标C中, cellForRowAtIndexPath 方法用于显示数据。 根据代码,您在 cellForRowAtIndexPath 中对ViewController B进行了调整。 它将多次分配ViewController B,从而导致内存消耗。
我建议你在ViewController A的ViewDidLoad中初始化。
要将数据从A传递到B,您需要使用UITableViewDelegate的 didSelectRowAtIndexPath 方法。
示例:
ViewController A:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 1st Type
ViewControllerB *vc=[[ViewController alloc]init];
vc.userIdInfo=[_List objectAtIndex:indexPath.row];
[self.navigartionController pushViewController:aVC animated:YES];
}
}
ViweController B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace SocrateMobile.Droid.Adapter
{
class PulledItemList_Adapter : BaseAdapter<oneimg_twolbl>
{
private Activity context;
private List<oneimg_twolbl> AllItemList;
public PulledItemList_Adapter(Activity context, List<oneimg_twolbl> AllItemList)
{
this.AllItemList = AllItemList;
this.context = context;
}
public oneimg_twolbl GetItem_bypos(int position)
{
return AllItemList[position];
}
public override oneimg_twolbl this[int position]
{
get { return AllItemList[position]; }
}
public override int Count
{
get { return AllItemList.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
Holder_oneimg_twolbl holder = null;
var view = convertView;
if (view != null)
holder = view.Tag as Holder_oneimg_twolbl;
if (holder == null)
{
holder = new Holder_oneimg_twolbl();
view = context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null);
holder.Text = view.FindViewById<TextView>(Resource.Id.text_list4_view);
holder.Text2 = view.FindViewById<TextView>(Resource.Id.text_list4_view2);
holder.Image = view.FindViewById<ImageButton>(Resource.Id.image_list4_view);
view.Tag = holder;
}
var current_item = AllItemList[position];
holder.Text.Text = current_item.FirstTxt;
holder.Text2.Text = current_item.SecondTxt;
holder.Image.SetImageResource(current_item.FirstImg);
holder.Image.Click += (sender, e) =>
{
int x = position;
};
return view;
}
public class Holder_oneimg_twolbl : Java.Lang.Object
{
public TextView Text { get; set; }
public TextView Text2 { get; set; }
public ImageButton Image { get; set; }
}
}
}