0

当用户选择任何行并且插入新行时,我想为 tableview 单元格显示一个自定义复选标记和取消复选标记图像,该单元格加载了未复选标记图像。

一旦我检查了该行,那么即使在使用新数据重新加载表之后,我也想显示以前检查过的行,而新的则未选中。

我怎样才能得到最后带有复选标记的所有行。

我试过了,但我无法在第一次加载自定义复选标记图像时我可以在取消选择行时进行更改,但它也有一些问题,因为它不能像我上面提到的那样始终如一地工作。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

 }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   return [cardData.mArrRecipient count];

 }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * sCellIdentifier = @"cell";
    NSLog(@"cellforrow");


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
    if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sCellIdentifier] autorelease];

    RecipientData *recipientData = [[cardData.mArrRecipient objectAtIndex:indexPath.row]autorelease];
    cell.textLabel.text = recipientData.sRecipientFName;
} 
cell.accessoryType=UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {

    if (IndexPath) 
    {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_checkmark_red.png"]] autorelease];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
        IndexPath=FALSE;
    }
    else 
    {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_uncheckmark_red.png"]] autorelease];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
        IndexPath=TRUE; 
    }
  }


  -(void)viewWillAppear:(BOOL)animated
  {

[super viewWillAppear:animated];   
if ([cardData.mArrRecipient count]>0) 
{
    [IBTblNewRecipientDetail reloadData];        
}
 }

谁可以帮我这个事??

4

2 回答 2

1

这是教程 http://weheartgames.com/2009/06/simple-iphone-checkbox/

或使用这个

  UIButton   * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
        checkBox.tag=indexPath.row;
        checkBox.frame=CGRectMake(270,15, 20, 20);
        [cell.contentView addSubview:checkBox];
        [checkBox setImage:[UIImage imageNamed:@"selectedBoxWith.png"] forState:UIControlStateNormal];
        [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];

-(void)checkBoxClicked:(id)sender{
if(self.isChecked ==NO){
  self.isChecked =YES;
  [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}else
{
   self.isChecked =NO;
  [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];

    }
}


and 

-(void)checkBoxClicked:(id)sender{

    UIButton *tappedButton = (UIButton*)sender;

    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"selectedBoxWith.png"]]) {
        [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
    }

    else {
        [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];
    }
}
于 2012-05-08T09:38:57.613 回答
0

用于选择多行:

- (void)addTableView {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
                                                        style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


self.timeSheetEntryTableView.allowsMultipleSelection = YES;

self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];

}

- (UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *cellIdentifier = @"Cell Identifier";


self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if (!self.cell) {

self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:cellIdentifier];
self.cell.accessoryType = UITableViewCellAccessoryNone;

}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
else
{
 [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}

return self.cell;

}

//是否选择和取消选择我使用了自定义单元格

 - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];

}

- (void)tableView:(UITableView *)tableView 
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array removeObject:indexPath];

[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];

}
于 2014-04-02T04:35:00.957 回答