2011-02-14 68 views
0

的文字我有2列在我的表:閱讀所選單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSUInteger const kLeftLabel =100; 
    static NSUInteger const kRightLabel=101; 

    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     CGRect leftF = CGRectMake(10, 5, 100, 30); 
     CGRect rightF = CGRectMake(120, 5, 100, 30); 

    left = [[[UILabel alloc] initWithFrame:leftF] autorelease]; 
    left.tag = kLeftLabel; // assumming #define kLeftLabel 100 
    [cell.contentView addSubview:left]; 

    right = [[[UILabel alloc] initWithFrame:rightF] autorelease]; 
    right.tag = kRightLabel;  

    [cell.contentView addSubview:right]; 

    } 
    else{ 
    left= (UILabel*)[cell.contentView viewWithTag:kLeftLabel]; 
    right = (UILabel*)[cell.contentView viewWithTag:kRightLabel]; 
    } 

    profileDB *Obj = [appDelegate.profileArray objectAtIndex:indexPath.row]; 

    left.text = Obj.profileName; 
    right.text = Obj.id; 

    return cell; 
} 

我想讀右列的文字只爲選定的單元格。

這可能嗎?

回答

3

首先,您必須找出所選單元格。

- (void) tableView:(UITableView*)tableview didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath]; 

    for(UILabel *lbl in [cell.contentView subviews]) 
    { 
     if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 101)) 
     { 
      NSString *str = lbl.textLabel.text; 

     } 

    } 
} 

如果已經添加了對細胞子視圖中,您可以通過使用他們的標籤

+0

我有2列,我想讀leftLable.text特定標籤的文本,我已經嘗試過這一個,但它給我錯誤 – Pooja 2011-02-14 12:30:26