【问题标题】:Accessing the IBOutlets's properties in the custom UITableViewCell with an IBAction使用 IBAction 访问自定义 UITableViewCell 中的 IBOutlets 属性
【发布时间】:2013-08-09 16:03:31
【问题描述】:

我是 Objective-c 和 iOS 的新手

我创建了几个带有插座的自定义表格单元格。但我不确定我是否使用了正确的方式。

我想要做的是通过表视图控制器中定义的 IBAction 获取自定义单元格内的 Outlet 的属性。

我有一个名为 KolonNumberCell 的自定义表格单元格,其中有一个 UISlider。 我想在 UITableViewController 中定义一个名为 playNumbers 的 IBAction。滑块在自定义单元格中似乎工作正常。我可以得到它的价值。

但我不知道如何在点击 playButton 插座时获取此滑块的值。

如果您能给我一些信息或告诉我方法,我将不胜感激。

提前致谢

TableController.h

#import <UIKit/UIKit.h>
@class KolonNumberCell;
@class BankoNumberCell;

@interface TableViewController : UITableViewController{
    NSArray *bundleArray;
    IBOutlet KolonNumberCell *kolonCell;
    IBOutlet BankoNumberCell *bankoCell;
    UIButton *playButton;
}

@property (strong) IBOutlet KolonNumberCell *kolonCell;
@property (strong) IBOutlet BankoNumberCell *bankoCell;
@property (nonatomic,retain) NSArray *bundleArray;
@property (nonatomic,retain) IBOutlet UIButton *playButton;

- (void)playNumbers:(id)sender;
@end

TableController.m 的一部分

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize bundleArray,kolonCell,bankoCell,playButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *bundle1 = [[[NSBundle mainBundle] loadNibNamed:@"KolonNumberCell" owner:self options:nil] objectAtIndex:0];
    NSArray *bundle2 = [[[NSBundle mainBundle] loadNibNamed:@"BankoNumberCell" owner:self options:nil] objectAtIndex:0];

    bundleArray = [[NSArray alloc] initWithObjects:bundle1,bundle2, nil];

}

- (void)playNumbers:(id)sender
{

    NSLog(@"button");
    // Get the value of the slider in KolonNumberCell... but how?

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    tableView.allowsSelection = NO;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [bundleArray objectAtIndex:indexPath.section];
    }

    return cell;

}
@end

我的一个 customtableviewcell 实现文件

#import "KolonNumberCell.h"

@implementation KolonNumberCell
@synthesize label,slider;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        // Initialization code

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

    self.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"doubleRound.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]];
}



- (IBAction)sliderChanged:(id)sender
{
    [label setText:[NSString stringWithFormat:@"%i",(int)slider.value]];
}

+ (NSString *)reuseIdentifier {
    return @"Cell";
}


@end

【问题讨论】:

    标签: ios uitableview ibaction


    【解决方案1】:

    你的设计很奇怪,因为你的代码会因为更多的单元而变得非常混乱。 Matt Gallagher 有一个很好的自定义 TableViews 实现。他使用 TableViewCell 作为控制器,并将所有与单元格相关的逻辑放在单元格类中。 Stack Overflow 上的讨论帖:

    CustomTableViewController

    如果你仍然想按照自己的方式做:

    1. 给你的 Slider 一个标签:slider.tag = x //确保标签是唯一的
    2. 在 PlayNumbers 中:

       UISlider *slider = (UISlider *)[[[bundleArray objectAtIndex:0] contentView] viewWithTag:x];
       NSLog(@"%i",(int)slider.value);
      

    编辑:感谢您更正语法

    【讨论】:

    • 我知道我应该了解有关自定义表格单元格的更多信息,但在此之前您的答案工作正常:D 我只是像 UISlider *slider = (UISlider *)[[[bundleArray objectAtIndex:0] contentView] viewWithTag:x];非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多