2011-02-14 64 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