2011-07-28 5 views
3

테이블 뷰의 셀에서 서브 뷰에 액세스하는 방법은 무엇입니까? "sliderValueChange"메서드에서 셀의 레이블에 액세스해야합니다.테이블 뷰의 셀에서 서브 뷰에 액세스하는 방법

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 

    } 
     cell.textLabel.text = @"Soglia"; 

     UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,168,23)] autorelease]; 
     slider.maximumValue = 70; 
     slider.minimumValue = 5; 
     [cell addSubview:slider]; 

     cell.accessoryView = slider; 
     [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]; 
     [slider release]; 

     UILabel *labelVal = [[UILabel alloc] initWithFrame:CGRectMake(218, 40, 30, 23)]; 
     labelVal.text = @"0"; 
     [cell addSubview:labelVal]; 

    return cell; 

} 

- (void)sliderValueChange:(id)sender { 
    UISlider *theSlider = (UISlider *)sender; 
    UITableViewCell *cell = (UITableViewCell *)theSlider.superview; 
    UITableView *tableView = (UITableView *)cell.superview; 

    //here I need to access to labelVal... 

} 

답변

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 

      //slider 
     UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,168,23)] autorelease]; 
     slider.maximumValue = 70; 
     slider.minimumValue = 5; 
     slider.tag=11; 
     [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]; 

     [cell.contentView addSubview:slider]; 



      //label 
     UILabel *labelVal = [[UILabel alloc] initWithFrame:CGRectMake(218, 40, 30, 23)]; 

     labelVal.text = @"0"; 

     labelVal.tag=22; 

     [cell.contentView addSubview:labelVal]; 




      //  cell.accessoryView = slider; 



    } 
    cell.textLabel.text = @"Soglia"; 





    return cell; 

} 

- (void)sliderValueChange:(id)sender { 

    UISlider *theSlider = (UISlider *)sender; 



    UIView *cell = (UIView *)theSlider.superview; 


    UILabel *label=(UILabel*)[cell viewWithTag:22]; 


    NSLog(@"label value is : %@ \n\n",label.text); 





} 
:

내 코드입니다
관련 문제