Hai,iam printing the checkbox for every row.But when we select the any checkbox only last cell checkbox will be selecting.Below one is the code for printing the checkboxes and daily,weekly,monthly data.
- (UITableViewCell *)tableView:(UITabl开发者_Go百科eView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc]init];
cell = [self getCellContentView:CellIdentifier];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3];
UILabel *lblTemp4 = (UILabel *)[cell viewWithTag:4];
checkButton=[UIButton buttonWithType:UIButtonTypeCustom];
checkButton.frame = CGRectMake(5,30, 15, 10);
[checkButton addTarget:self action:@selector(SelectCheckBox) forControlEvents:UIControlEventTouchUpInside];
pictureimageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 60)];
//pictureimageView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 3, 65, 65)];
[pictureimageView.layer setBorderColor:[[UIColor grayColor] CGColor]];
[pictureimageView.layer setShadowColor:[[UIColor grayColor] CGColor]];
[pictureimageView.layer setMasksToBounds:YES];
[pictureimageView.layer setBorderWidth:1.5];
[pictureimageView.layer setCornerRadius:5.0];
[pictureimageView.layer setShadowRadius:1.0];
[pictureimageView.layer setShadowOpacity:3.0];
if(selectedSegment==0)
{
databasefields=[eventData objectAtIndex:indexPath.row];
lblTemp1.text=databasefields.DBtitle;
lblTemp2.text=databasefields.DBlocation;
lblTemp3.text=databasefields.DBdate ;
lblTemp4.text=databasefields.DBtime;
//NSString *pictureString=[[NSString alloc]init];
pictureString=databasefields.DBpicture;
checkimage = [UIImage imageNamed:@"checkbox.PNG"];
[checkButton setImage:checkimage forState:UIControlStateNormal];
pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
[cell.contentView addSubview:checkButton];
[cell.contentView addSubview:pictureimageView];
}
else
if(selectedSegment==1)
{
databasefields=[eventData objectAtIndex:indexPath.row];
lblTemp1.text=databasefields.DBtitle;
lblTemp2.text=databasefields.DBlocation;
lblTemp3.text=databasefields.DBdate;
lblTemp4.text=databasefields.DBtime;
pictureString=[[NSString alloc]init];
pictureString=databasefields.DBpicture;
checkimage = [UIImage imageNamed:@"checkbox.PNG"];
pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
[cell.contentView addSubview:checkButton];
[cell.contentView addSubview:pictureimageView];
}
else if(selectedSegment==2)
{
databasefields=[eventData objectAtIndex:indexPath.row];
lblTemp1.text=databasefields.DBtitle;
lblTemp2.text=databasefields.DBlocation;
lblTemp3.text=databasefields.DBdate;
lblTemp4.text=databasefields.DBtime;
pictureString=[[NSString alloc]init];
pictureString=databasefields.DBpicture;
checkimage = [UIImage imageNamed:@"checkbox.PNG"];
[checkButton setImage:checkimage forState:UIControlStateNormal];
pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
[cell.contentView addSubview:checkButton];
[cell.contentView addSubview:pictureimageView];
}
// Configure the cell...
return cell;
}
-(void) SelectCheckBox
{
if (checkboxSelected == 0){
[checkButton setSelected:YES];
checkimage = [UIImage imageNamed:@"checkbox-on.PNG"];
[checkButton setImage:checkimage forState:UIControlStateNormal];
checkboxSelected = 1;
} else {
[checkButton setSelected:NO];
checkimage = [UIImage imageNamed:@"checkbox.PNG"];
[checkButton setImage:checkimage forState:UIControlStateNormal];
checkboxSelected = 0;
}
;
}
Please give me the solution for selecting the every checkbox
Please read the Table View Programming Guide to learn about cells. In this case, it looks like checkButton
is an instance variable of the table delegate, and that's what you're modifying in your -SelectCheckBox
method. However, that variable only points to the button in the cell that you created most recently. If you want to operate on a particular cell, you should first get the cell for that row, and then find its check box button. One way to do that is to set the tag
property to a particular value when you set up the cell. Then you can ask the cell for the subview that has that tag.
精彩评论