UITableview加载ios中带有复选标记的默认选定值



我有一个为多选行的表视图设置值的方法

- (id)initWithTitle:(NSString *)aTitle options:(NSArray *)aOptions matchingArray:(NSArray *)matchArray xy:(CGPoint)point size:(CGSize)size isMultiple:(BOOL)isMultiple
{
isMultipleSelection=isMultiple;
float height = MIN(size.height, DROPDOWNVIEW_HEADER_HEIGHT+[aOptions count]*44);
CGRect rect = CGRectMake(point.x, point.y, size.width, height);
if (self = [super initWithFrame:rect])
{
    self.backgroundColor = [UIColor clearColor];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(2.5, 2.5);
    self.layer.shadowRadius = 2.0f;
    self.layer.shadowOpacity = 0.5f;
    _kTitleText = [aTitle copy];
    _kDropDownOption = @[@"India",@"Swaziland",@"Africa",@"Australlia",@"Pakistan",@"Srilanka",@"Mexico",@"United Kingdom",@"United States",@"Portugal"];
    _kMatchingArray = @[@"United States",@"Swaziland"];
    finalarray=[[NSMutableArray alloc]init];

    for(int i = 0;i<[_kMatchingArray count];i++)
    {
        for(int j= 0;j<[_kDropDownOption count];j++)
        {
            if([[_kMatchingArray objectAtIndex:i] isEqualToString:[_kDropDownOption objectAtIndex:j]])
            {
                NSLog(@"%d",j);
                NSString *str = [NSString stringWithFormat:@"%d",j];
                [finalarray addObject:str];
            }
            else {
            }
        }
    }
    NSLog(@"finalArray:%@",finalarray);
  //  NSLog(@"%@",_kMatchingArray);
    self.arryData=[[NSMutableArray alloc]init];
    _kTableView = [[UITableView alloc] initWithFrame:CGRectMake(DROPDOWNVIEW_SCREENINSET,
                                                               DROPDOWNVIEW_SCREENINSET + DROPDOWNVIEW_HEADER_HEIGHT,
                                                               rect.size.width - 2 * DROPDOWNVIEW_SCREENINSET,
                                                               rect.size.height - 2 * DROPDOWNVIEW_SCREENINSET - DROPDOWNVIEW_HEADER_HEIGHT - RADIUS)];
    _kTableView.separatorColor = [UIColor colorWithWhite:1 alpha:.2];
    _kTableView.separatorInset = UIEdgeInsetsZero;
    _kTableView.backgroundColor = [UIColor clearColor];
    _kTableView.dataSource = self;
    _kTableView.delegate = self;
    [self addSubview:_kTableView];
    if (isMultipleSelection) {
        UIButton *btnDone=[UIButton  buttonWithType:UIButtonTypeCustom];
        [btnDone setFrame:CGRectMake(rect.origin.x+182,rect.origin.y-45, 82, 31)];
        [btnDone setImage:[UIImage imageNamed:@"done@2x.png"] forState:UIControlStateNormal];
        [btnDone addTarget:self action:@selector(Click_Done) forControlEvents: UIControlEventTouchUpInside];
        [self addSubview:btnDone];
    }

}
return self;
   }

使用这个,我创建了一个获取值的表视图

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
           return [_kDropDownOption count];
      }
 - (UITableViewCell *)tableView:(UITableView *)tableView cel lForRowAtIndexPath:(NSIndexPath *)indexPath
   {
static NSString *cellIdentity = @"DropDownViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
cell = [[DropDownViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
     NSInteger row = [indexPath row];
   // NSIndexPath *selectedIndexPath = [finalarray addObject:indexPath.row];
UIImageView *imgarrow=[[UIImageView alloc]init ];
NSLog(@"aray:%@",self.arryData);
if([self.arryData containsObject:indexPath]){
    imgarrow.frame=CGRectMake(230,2, 27, 27);
    imgarrow.image=[UIImage imageNamed:@"check_mark@2x.png"];
} else
    imgarrow.image=nil;
[cell addSubview:imgarrow];
cell.textLabel.text = [_kDropDownOption objectAtIndex:row] ;
return cell;
   }
   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  if (isMultipleSelection) {
    if([self.arryData containsObject:indexPath]){
        [self.arryData removeObject:indexPath];
    } else {
        [self.arryData addObject:indexPath];
    }
    [tableView reloadData];
} else {
    if (self.delegate && [self.delegate respondsToSelector:@selector(DropDownListView:didSelectedIndex:)]) {
        [self.delegate DropDownListView:self didSelectedIndex:[indexPath row]];
    }
    // dismiss self
    [self fadeOut];
}
 }

我有两个数组,一个是表视图的总记录,另一个是最初选择的值。我比较了这两个数组,得到了匹配的索引路径。我的问题是如何在匹配值行上设置复选标记图像?

if ([_strProIDNS isEqualToString:strUNAssignNS])
        {
            tblViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
            tblViewCell.accessoryType = UITableViewCellAccessoryNone;

最新更新