Xcode4 将UItableviewcell的UILabel值传递给iPhone中的另一个视图,返回空值?
我是objective-C的新手,在某个特定点上遇到了麻烦。当accessoryButtonTappedForRowWithIndexPath操作发生时,我必须将UILabel值从tableviewcell传递到Scrollview中的标签。但该值没有传递。我不知道我哪里出错了?我在写这段代码:Xcode4 将UItableviewcell的UILabel值传递给iPhone中的另一个视图,返回空值?,xcode4,uitableview,Xcode4,Uitableview,我是objective-C的新手,在某个特定点上遇到了麻烦。当accessoryButtonTappedForRowWithIndexPath操作发生时,我必须将UILabel值从tableviewcell传递到Scrollview中的标签。但该值没有传递。我不知道我哪里出错了?我在写这段代码: ViewController1.h: UILabel *name1; @property(nonatomic,retain)IBOutlet UILabel *name1; ViewC
ViewController1.h:
UILabel *name1;
@property(nonatomic,retain)IBOutlet UILabel *name1;
ViewController1.m:
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
ViewController2 *v2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:v2 animated:YES];
v2.provName=[name1 retain]; //name1 is the name of UILabel in TableView.
[v2 release];
}
ViewController2.h
UILabel *providerName;
SString *provName;
ViewController2.m:
- (void)viewDidLoad
{
providerName =[[UILabel alloc] init];
[providerName setFrame:CGRectMake(10,10,300,50) ];
providerName.textAlignment=UITextAlignmentLeft;
providerName.backgroundColor=[UIColor blackColor];
self.providerName.text=self.provName;
providerName.highlightedTextColor=[UIColor whiteColor];
[self.view addSubview:providerName];
}
我可以看到标签,但看不到其中的值…是这样吗?如何将UIlabel值传递给另一个视图?在
附件按钮中,只需按如下所示进行一些更改
加
就在上面
[self presentModalViewController:v2 animated:YES];
而且由于V2中的provName是合成的,所以无需保留
只需分配它即可
编辑:要获取单元格,请使用下面的
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
v2.provName = cell.name1;
UITableViewCell
也可以用于自定义单元格
编辑:更改CellForRow
将您的cellForRow
更新为
-(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];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel* name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 10)];
name1.font=[UIFont boldSystemFontOfSize:14];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:@"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
return cell;
}
不要将单元格和名称1设置为全局,仅在cellForRow
更新您的didSelectRow
,如下所示
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;
这应该行得通
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;