简体   繁体   English

带有UISegmentedControl的iPhone自定义单元

[英]iPhone Custom cell with UISegmentedControl

I have created a custom cell with a UISegmentedControl on it and loaded the cell like, 我创建了一个带有UISegmentedControl的自定义单元,并像这样加载了该单元,

static NSString *CellIdentifier = @"Cell";

SegmentedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    NSArray *cells =[[NSBundle mainBundle] loadNibNamed:@"SegmentedCell" owner:nil options:nil];

    for (UIView *view in cells) 
    {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (SegmentedCell *)view;
            [cell.SegmentedControl addTarget:self
                                                  action:@selector(segmentedControlChanged:)
                                        forControlEvents:UIControlEventValueChanged];        


        }
    }
}

cell.textLabel.text = @"Sample";
cell.selectionStyle = UITableViewCellSelectionStyleNone;

The custom cell successfully loaded and I get the action of the SegmentedControl. 自定义单元已成功加载,我得到了SegmentedControl的操作。 But when I scroll the table view, state of the SegmentedControl is changed. 但是,当我滚动表视图时,SegmentedControl的状态会更改。

When you scroll a tableview that uses dequeueReusableCellWithIdentifier, you aren't actually saving all the different cells. 滚动使用dequeueReusableCellWithIdentifier的表视图时,实际上并没有保存所有不同的单元格。 To fix your problem you need to implement a few things. 要解决您的问题,您需要执行一些操作。

  • Set your segmented control up to store its value to a variable every time it is pressed. 设置分段控件,以便每次按下时将其值存储到变量中。
  • Put this value into an array 将此值放入数组
  • In the cellForRowAtIndexPath method, get the variable out of the array and set the segmented control's value to this value of the variable. 在cellForRowAtIndexPath方法中,将变量从数组中取出,并将分段控件的值设置为该变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM