0

私は客観的 C の初心者で、特定の時点で打たれました.accessoryButtonTappedForRowWithIndexPath アクションが発生したときに、UILabel 値を tableviewcell から 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の値を別のビューに渡すにはどうすればよいですか?

4

1 に答える 1

0

accessoryButtonTappedForRowWithIndexPath以下のようにいくつかの変更を加えるだけで、

追加

v2.provName=[name1 retain];   //name1 is the name of UILabel in TableView.

すぐ真上に

[self presentModalViewController:v2 animated:YES];

また、V2 の provName は合成されるため、retain単に割り当てる必要はありません。

編集:セルを取得するには、以下を使用します

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
v2.provName = cell.name1;

UITableViewCellカスタムセルもできます。

編集:変更CellForRow

cellForRowas を更新する

-(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;
}

cell と name1 をグローバルにしないでください。cellForRow

didSelectRow以下のように更新します

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;

これはうまくいくはずです。

于 2012-10-09T17:55:22.087 に答える