使用协议检测UISegmentedControl中的值更改

时间:2016-04-25 05:57:38

标签: ios xcode uisegmentedcontrol swift-protocols

我在UITableViewCell中有一个UISegmentedControl,我需要监听TableViewController中的值更改。

到目前为止,我知道我必须使用协议才能做到这一点,但我不确定在哪里放置代码。它应该在UITableViewCell中吗?在ViewController中?这对我来说有点混乱。

我需要一些关于从哪里开始,甚至更好的一些代码示例的精确定位?

3 个答案:

答案 0 :(得分:3)

根据我们在评论中讨论的内容,您希望每次用户更改UITableView选项时重新加载UISegmentedControl

首先,您需要将其添加到UISegmentedControl

self.segmentedControl.addTarget(self, action: "segmentedControlIndexChanged", forControlEvents: .ValueChanged)

但是这只是在UISegmentedControl启动后执行,而不是在第二个回答中的cellForRow中执行,因为它会在每次重复使用时被调用(如果你有更多的单元格,这将会很多可以出现在屏幕上。)

然后你用这种方法做逻辑:

func segmentedControlIndexChanged() {
    // change your UITableView data or even swap your UITableViews (if you have 2)
}

请记住,当您手动更改索引时,您还需要手动调用事件更改:

segmentedControl.sendActionsForControlEvents(.ValueChanged)

答案 1 :(得分:0)

您可以在tableViewController中添加方法。

在UITableViewController的cellForRow方法中添加以下代码。

[cell.yourSegmentControl addTarget:self action:@selector(handleSegmentControlEvent:) forControlEvents: UIControlEventValueChanged];

然后在同一个控制器中实现方法,

- (void)handleSegmentControlEvent:(id)sender{
}

希望它有所帮助。

答案 2 :(得分:0)

Here is the code using protocol.In this sample , you can observe the value change of the segmentControl. You can know which cell's segment being tapped and know the segment data change.

// ViewController.m

#import "ViewController.h"
#import "segmentTestCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, segmentTestCellDelegate>

@property (nonatomic, strong) UITableView *table;

@end

@implementation ViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.table registerClass:[segmentTestCell class] forCellReuseIdentifier:@"cellId"];
    [self.view addSubview:self.table];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource, UITabBarDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    segmentTestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    cell.indexPath = indexPath;
    if (!cell.delegate) {
        cell.delegate = self;
    }
    return cell;
}

#pragma mark - segmentTestCellDelegate
- (void)segmentChangeAtIndexPath:(NSIndexPath *)indexPath SelectIndex:(NSInteger)selectedIndex SelectTitle:(NSString *)selectedTitle {
    NSLog(@"%lu - %lu - %@",indexPath.section,indexPath.row,selectedTitle);

    // do something by yourself
}

#pragma mark - Getter
- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _table.allowsSelection = NO;
        _table.dataSource = self;
        _table.delegate = self;
    }
    return _table;
}

@end

// segmentTestCell.h

#import <UIKit/UIKit.h>

@protocol segmentTestCellDelegate <NSObject>
@optional
- (void)segmentChangeAtIndexPath: (NSIndexPath *)indexPath SelectIndex: (NSInteger)selectedIndex SelectTitle: (NSString *)selectedTitle;
@end

@interface segmentTestCell : UITableViewCell

@property (nonatomic, strong) UISegmentedControl *segmentControl;

@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, weak) id<segmentTestCellDelegate> delegate;

@end

// segmentTestCell.m

#import "segmentTestCell.h"

#define titles @[@"A",@"B",@"C"]

@interface segmentTestCell()

@end

@implementation segmentTestCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.segmentControl.frame = CGRectMake(20, 10, 100, 30);
        [self.contentView addSubview:self.segmentControl];
    }
    return self;
}

- (void)segmentValueChange: (id)sender {
    // do something
    UISegmentedControl *control = (UISegmentedControl *)sender;
    if ([self.delegate respondsToSelector:@selector(segmentChangeAtIndexPath:SelectIndex:SelectTitle:)]) {
        [self.delegate segmentChangeAtIndexPath:_indexPath SelectIndex:control.selectedSegmentIndex SelectTitle:titles[control.selectedSegmentIndex]];
    }
}

- (UISegmentedControl *)segmentControl {
    if (!_segmentControl) {
        _segmentControl = [[UISegmentedControl alloc]initWithItems:titles];
        [_segmentControl addTarget:self action:@selector(segmentValueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentControl;
}

@end