objective-c - 如何访问tableview单元格中的 subview

标签 objective-c cocoa-touch uitableview

如何访问tableview单元格中的 subview ? 在“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...

}

最佳答案

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





}

关于objective-c - 如何访问tableview单元格中的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859865/

相关文章:

ios - 为什么我看不到我的 TableViewRowActions 按钮(快速)?

objective-c - 从 UIView 的 subview 获取 UINavController?

ios - 在 Storyboard 中推送多个 View Controller

ios - 如何为 UINavigationBar 设置不透明的 1px 高阴影图像?

iphone - 如何以编程方式渲染平面背景 UIColor 作为 UIToolbar 的背景?

ios - 我如何编辑表格 View 部分标题 ios swift

ios - UITableview cellForRowAtIndexPath 方法没有执行?

objective-c - iOS 8 和 XCode 6 上的 AVFoundation 语音合成

ios - 可以在 UICollectionView 中自定义单元格吗?

iOS:使用应用委托(delegate)来存放全局变量