我是Objective-C的新手,并且在某个特定点被攻击。当必须将accessoryButtonTappedForRowWithIndexPath动作发生时,我必须将来自tableviewcell的UILabel值传递给Scrollview中的标签。但是该值未通过..我不知道在哪里我错了?我正在写这段代码:
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值传递给另一个视图?
答案 0 :(得分:0)
在您的accessoryButtonTappedForRowWithIndexPath
中进行如下更改,
添加
v2.provName=[name1 retain]; //name1 is the name of UILabel in TableView.
正好在
之上[self presentModalViewController:v2 animated:YES];
并且当V2中的provName被合成时,不需要retain
只是分配它..
编辑:要使Cell使用以下
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;
}
不要将您的单元格和name1设为全局,仅在cellForRow
更新您的didSelectRow
,如下所示
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;
这应该可以正常工作。