从tableviewcells获取segmentedcontrol值

时间:2013-12-14 23:35:11

标签: ios xcode uitableview ios7 uisegmentedcontrol

如何从tableviewcells中的segmentedcontrols获取值(选定状态)? 当我按下“获取状态”按钮时,它应该返回每个分段控件的值。我尝试了不同的方法,但我找不到一个有效的方法:(

到目前为止我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];

tableData = [[NSMutableArray alloc] initWithCapacity:0];
tableData = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"StateCell"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StateCell"];
}

//Config cell..
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

NSArray *itemArray = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(110, 7, 100, 28);

[cell.contentView addSubview:segmentedControl];

return cell;
[[self tableView] reloadData];
}

- (IBAction)getStates:(id)sender {

// Ruturn the current selected statement, for the individual cell's segmentedcontrol..
// Ex. First = SelectedState 1, Second = SelectedState 0 & Third = SelectedState 2..

}

所以我真正想要的是;是“获取状态”按钮操作必须做的事情..

谢谢你的时间!

Screenshot of what i mean..

3 个答案:

答案 0 :(得分:0)

使用数组存储所有段控制值,单击一个段控件时,只需相应更改值即可。

答案 1 :(得分:0)

你在这里重复使用会遇到严重问题

不要在tableView:cellForRowAtIndexPath:方法中分配新的UI元素,除非它处于if条件if (cell == nil)

更改tableView:cellForRowAtIndexPath:中的内容

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StateCell"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StateCell"];

    // Add the Segmented Control
    NSArray *itemArray = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(110, 7, 100, 28);
    segmentedControl.tag = 1;
    [cell addSubview:segmentedControl];
}

//Config cell..
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

// Get that Segmented Control
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell viewWithTag:1];
segmentedControl.selectedSegmentIndex = 0; // Set your default value here or add your data in an array and read from that array

return cell;

然后在按钮的操作中执行

for (UITableViewCell *cell in [tableView visibleCells]) {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell viewWithTag:1];
    NSLog(@"%d",segmentedControl.selectedSegmentIndex);
}

但是,除非表中只有3个单元格以避免重用或可见性问题,否则此代码并不完美

答案 2 :(得分:0)

您的代码有几个问题。其中大多数是因为UITableView重用其单元格而发生的。

每次显示一个单元格时,您都在创建一个新的UISegmentedControl,您不应该这样做。只有在创建单元格时才应创建UISegmentedControl,将该代码移动到cell == nil)

您没有可以保存段状态的dataSource。您不应该在视图中保存状态,尤其是在处理tableView时,因为不会重复使用单元格。

这是一个可以获得所需功能的示例。

// this is an object of your model, it has a title and saves the selected index
@interface MBFancyObject : NSObject
@property (strong, nonatomic) NSString *title;
@property (assign, nonatomic) NSInteger selectedIndex;
@end

@implementation MBFancyObject
@end


@interface MasterViewController () {
    NSMutableArray *_objects;     // stores instances of MBFancyObject
}
@end

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // set up the model
    _objects = [NSMutableArray array];
    for (NSInteger i = 0; i < 6; i++) {
        MBFancyObject *object = [[MBFancyObject alloc] init];
        object.title = [NSString stringWithFormat:@"Object #%ld", (long)i];
        object.selectedIndex = i % 3;
        [_objects addObject:object];
    }

    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Get States" style:UIBarButtonItemStyleBordered target:self action:@selector(logStates:)];
    self.navigationItem.rightBarButtonItem = button;
}

#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FancyCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FancyCell"];
        // add the segmentedControl when you create a new cell
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"1", @"2", @"3"]];
        segmentedControl.frame = CGRectMake(110, 7, 100, 28);
        [cell.contentView addSubview:segmentedControl];

        // add an action so we can change our model if the view changes
        [segmentedControl addTarget:self action:@selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];

        // use a tag so we can retrieve the segmentedControl later
        segmentedControl.tag = 42;
    }
    // either if the cell could be dequeued or you created a new cell,
    // segmentedControl will contain a valid instance 
    UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];

    MBFancyObject *object = _objects[indexPath.row];
    cell.textLabel.text = object.title;
    segmentedControl.selectedSegmentIndex = object.selectedIndex;
    return cell;
}

- (IBAction)didChangeSegmentedControl:(UISegmentedControl *)sender {
    // transform the origin of the cell to the frame of the tableView
    CGPoint senderOriginInTableView = [self.tableView convertPoint:CGPointZero fromView:sender];

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
    NSAssert(indexPath, @"must have a valid indexPath");
    MBFancyObject *object = _objects[indexPath.row];
    object.selectedIndex = sender.selectedSegmentIndex;
}

- (IBAction)logStates:(id)sender {
    // query the model, not the view
    for (NSInteger i = 0; i < [_objects count]; i++) {
        MBFancyObject *object = _objects[i];
        NSLog(@"Object \"%@\" - %ld", object.title, (long)object.selectedIndex);

        // since you have only one section, each indexPath is 0,i
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    }
}

@end