I am using a custom cell on UITableView with multi Section and i’m added a UITapGestureRecognizer to a UIImageView for change it when the image is selected but When I select the image other's images in different cell are selected as well.
This is the code for the TableView datasource method cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *text = (UILabel *)[cell viewWithTag:ROW_TITLE];
UIImageView *image = (UIImageView *)[cell viewWithTag:ROW_IMAGE];
NSDictionary *item = [results objectAtIndex:indexPath.section];
NSString *string = [item objectForKey:JSONResp_common_name];
[text setText:((string != [NSNull null])? string : #"")];
if ([[item objectForKey:JSONResp_common_status] isEqualToNumber:[NSNumber numberWithBool:YES]])
[image setImage:[UIImage imageNamed:#"checked"]];
else
[image setImage:[UIImage imageNamed:#"unchecked"]];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
[image setTag:indexPath.row];
[image setUserInteractionEnabled:YES]; action:#selector(handleTapFrom:)];
if([self tableView:tableView
canCollapseSection:indexPath.section]){ // EXPANDABLE ROW
if(!indexPath.row){// HEADER
// SET ACCESORIES
if([expandedSections containsIndex:indexPath.section])
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]]];
else
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]]];
[image setUserInteractionEnabled:NO];
//Remove checked option from a cell
for(UITapGestureRecognizer *tapGesture in cell.gestureRecognizers)
[cell removeGestureRecognizer:tapGesture];
}else{// SUB ROWS
NSArray *subValues = [item objectForKey:PARAM_STRUCT_SUBELEMENTS];
NSDictionary *detail = [subValues objectAtIndex:indexPath.row -1];
//REASING VALUES TO SUB VALUES
if(detail && [detail isKindOfClass:[City class]]){
City *currentCity = (City *)detail;
[text setText:[currentCity name]];
}
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
}else{//ROWS CHECKED OPTION
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
return cell;
}
</code>
</pre>
This is the code for the TableView datasource method didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *data = [self.results objectAtIndex:indexPath.section];
if([self tableView:tableView
canCollapseSection:indexPath.section]){
if(!indexPath.row){// EXPANDABLE
// only first row toggles expandaed/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if(currentlyExpanded){
rows = [self tableView:tableView
numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}else{
[expandedSections addIndex:section];
rows = [self tableView:tableView
numberOfRowsInSection:section];
}
for(int i = 1; i < rows; i++){
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(currentlyExpanded){
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]];
}else{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]];
}
}else{// Detail
}
}else{ // Normal Row
//[]
}}
This is the code for the TableView datasource method fro the UITapGestureRecognizer
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = (UITableViewCell *)[recognizer view];
UIImageView *switchImageView = nil;
for (UIView *item in [[[cellView subviews] firstObject] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
if (switchImageView == nil) {
for (UIView *item in [[[[[cellView subviews] firstObject] subviews] objectAtIndex:1] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
}
NSDictionary *item = [results objectAtIndex:switchImageView.tag];
if ([Utility image:switchImageView.image
isEqualTo:[UIImage imageNamed:IMAGE_UNCHECKED]]) {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_sortings]) {
[delegate didCheckFilter:item
withStatus:YES];
} else if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_CHECKED]]; //change to a selected image
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:YES];
}
} else {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_UNCHECKED]];
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:NO];
}
}}
While declaring the UITapGestureRecognizer on particular cell assign the tag as
cell.tag = indexPath.row;
[cell addGestureRecognizer:tapGestureRecognizer];
and in your handler do like this
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.tag inSection:0]];
// your stuff
}
Hope this Helps!
Related
I have a strange behavior my tableView
I make dropdown section on my tableView. It's look like:
but if I reopen my section rows order was ather like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
NSLog(#"CURRENT ROW = %ld", (long)indexPath.row);
UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, cell.frame.size.width - 90, 20)];
titleLable.text = [NSString stringWithFormat:#"урок %ld", (long)indexPath.row];
[cell addSubview:titleLable];
UILabel *percentLable = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width - 40, 20, 50, 20)];
LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
percentLable.text =[NSString stringWithFormat:#"%# %%", [childrenArrey[indexPath.row] valueForKey:#"percent"]];
[cell addSubview:percentLable];
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];
NSString *uid = [childrenArrey[indexPath.row] valueForKey:#"id"];
[self makeLessonWithId:uid];
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[self.arrayForBool objectAtIndex:indexPath.section] boolValue];
collapsed = !collapsed;
[self.arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
[self updateLabelFrame:gestureRecognizer.view.tag];
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
}
Try by putting values in else part too:
if (cell == nil){
//your code as it is
}
else{
titleLable.text = [NSString stringWithFormat:#"урок %ld", (long)indexPath.row];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
percentLable.text =[NSString stringWithFormat:#"%# %%", [childrenArrey[indexPath.row] valueForKey:#"percent"]];
}
I am new in iOS developer. When I run the the app than time iI
but I will get this type of output.
Here is my code
- (void) iterateItems:(NSArray*) array
{
level++;
for (int i = 0; i < [array count]; i++)
{
commentInfo = [array objectAtIndex:i];
//NSLog(#"data=%#",commentInfo);
Item *item = [[Item alloc] init];
item.Id = [[[commentInfo objectForKey:#"sub_items"]objectAtIndex:0]objectForKey:#"name"];
item.parentId = [[[commentInfo objectForKey:#"sub_items"]objectAtIndex:0]objectForKey:#"name"];
if (item.level == 0)
{
item.title =[commentInfo objectForKey:#"dates"];
item.title1 =[commentInfo objectForKey:#"id"];
}
else
{
item.title=#"abc";
}
//NSLog(#"title is=%#",[commentInfo objectForKey:#"status"]);
item.level = level;
item.visibility = #"normal";
item.childVisibility = #"normal";
[items addObject:item];
[self iterateItems:[commentInfo objectForKey:#"sub_items"]];
}
level--;
}
- (float) cellHeightForRow:(int) row
{
Item *item = [items objectAtIndex:row];
if ([item.visibility isEqualToString:#"hidden"])
{
return 0.0f;
}
else
{
return 50.0f;
}
}
- (void) hideCompleteNode:(UISwipeGestureRecognizer*) gestureRecognizer
{
UITableViewCell *cell = (UITableViewCell*) [[gestureRecognizer view] superview];
NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
Item *item = [items objectAtIndex:indexPath.row];
if (item.level == 0)
{
if ([item.childVisibility isEqualToString:#"normal"])
{
[self hideChilds:indexPath];
}
}
else
{
int prevIndex = indexPath.row-1;
if (prevIndex >= 0)
{
Item *parentItem = [items objectAtIndex:prevIndex];
while (parentItem.level != 0)
{
prevIndex--;
if (prevIndex >= 0)
parentItem = [items objectAtIndex:prevIndex];
else
break;
}
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:prevIndex inSection:0];
[self hideChilds:indexPath];
}
}
- (void) hideChilds:(NSIndexPath*) indexPath
{
NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithObjects:indexPath, nil];
Item *item = [items objectAtIndex:indexPath.row];
BOOL shouldHide = [item.childVisibility isEqualToString:#"hidden"]?NO:YES;
item.childVisibility = shouldHide?#"hidden":#"normal";
int nextIndex = indexPath.row+1;
if (nextIndex < [items count])
{
Item *childItem = [items objectAtIndex:nextIndex];
while (childItem.level > item.level)
{
childItem.visibility = shouldHide?#"hidden":#"normal";
childItem.childVisibility = shouldHide?#"hidden":#"normal";
[indexPaths addObject:[NSIndexPath indexPathForRow:nextIndex inSection:0]];
nextIndex++;
if (nextIndex < [items count])
childItem = [items objectAtIndex:nextIndex];
else
break;
}
}
[self.tblView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark -
#pragma mark - UITableView Datasource and Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self cellHeightForRow:indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"itemCell";
UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIImageView *arrowImgView = [[UIImageView alloc] init];
[arrowImgView setBackgroundColor:[UIColor clearColor]];
[arrowImgView setTag:1];
[cell.contentView addSubview:arrowImgView];
UILabel *titleLbl = [[UILabel alloc] init];
[titleLbl setFont:[UIFont fontWithName:#"HelveticaNeue" size:13.0f]];
[titleLbl setTextColor:[UIColor blackColor]];
titleLbl.textAlignment = NSTextAlignmentCenter;
[titleLbl setTag:2];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:titleLbl];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideCompleteNode:)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell.contentView addGestureRecognizer:swipeGesture];
}
Item *item = [items objectAtIndex:indexPath.row];
if (item.level == 0)
{
[cell.contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_background.png"]]];
}
else
{
[cell.contentView setBackgroundColor:[UIColor clearColor]];
}
UIImageView *arrowImgView = (UIImageView*) [cell viewWithTag:1];
UILabel *titleLbl = (UILabel*) [cell viewWithTag:2];
if ([item.visibility isEqualToString:#"hidden"])
{
[titleLbl setFrame:CGRectZero];
[arrowImgView setFrame:CGRectZero];
}
else
{
[arrowImgView setFrame:CGRectMake(5.0f+(20.0f*item.level), 20.0f, 10.0f, 10.0f)];
if ([item.childVisibility isEqualToString:#"hidden"])
{
[arrowImgView setImage:[UIImage imageNamed:#"closeArrow"]];
}
else
{
[arrowImgView setImage:[UIImage imageNamed:#"openArrow"]];
}
[titleLbl setFrame:CGRectMake(20.0f+(20.0f*item.level), 10.0f, 320.0f - (20.0f+(20.0f*item.level)), 30.0f)];
[titleLbl setText:item.title];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.layer.shadowOpacity = 0.3;
cell.layer.shadowColor = [UIColor blueColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0.0, 4);
[tblView setSeparatorColor:[UIColor redColor]];
tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[tblView setSeparatorColor:[UIColor blackColor]];
tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// [self hideChilds:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[self.tblView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:0.004 green:0.482 blue:0.776 alpha:1];
[self hideChilds:indexPath];
Item *item = [items objectAtIndex:indexPath.row];
if (item.level == 0)
{
}
else
{
Item *item = [items objectAtIndex:indexPath.row];
NSLog(#"value=%#",[NSString stringWithFormat:#"%#",item.title1]);
approveleaseoffer *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"approveleaseofferpage"];
secondViewController.offer_number=[NSString stringWithFormat:#"%#",item.title1];
[self.navigationController pushViewController:secondViewController animated:YES];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:#selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)viewDidLayoutSubviews
{
if ([tblView respondsToSelector:#selector(setSeparatorInset:)]) {
[tblView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tblView respondsToSelector:#selector(setLayoutMargins:)]) {
[tblView setLayoutMargins:UIEdgeInsetsZero];
}
}
Try changing the line item.childVisibility = #"normal"; to item.childVisibility = #"hidden";
in iterateItems method.
I have two collectionViews. A collectionViewController and another collectionView in the header. When I scroll down the collectionViewController, the collectionView cells of the the header collectionView disappear. There is also a segControl in the header that changes the collectionViewController cells and this also makes the header collectionView cells disappear. When the controller appears all the cells are present, BUT when i wither scroll of select the segControl the header Collection View cells disappear. The collectionViewController works fine, just the one in the header is messed up.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (collectionView == self.collectionView) {
return [self.dataArray count];
}
return [self.groupArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView != self.collectionView) {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"socialCell" forIndexPath:indexPath];
PFObject *group = [self.groupArray objectAtIndex:indexPath.row];
UILabel *label = (UILabel *) [cell viewWithTag:55];
label.text = [group objectForKey:#"Title"];
return cell;
}
userPostCell *postCell = (userPostCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"postCell" forIndexPath:indexPath];
userPictureCell *pictureCell = (userPictureCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"pictureCell" forIndexPath:indexPath];
userEventCell *eventCell = (userEventCell *) [collectionView dequeueReusableCellWithReuseIdentifier:#"eventCell" forIndexPath:indexPath];
PFObject *temp = [self.dataArray objectAtIndex:indexPath.row];
if (self.dataArray == self.postsArray) {
postCell.postLabel.text = [temp objectForKey:#"stringPost"];
[self changeToCircle:postCell.profileImage];
if ([temp objectForKey:#"Event"] == nil) {
[postCell.noEventNameButton setTitle:[temp objectForKey:#"User_Name"] forState:UIControlStateNormal];
postCell.noEventNameButton.tag = indexPath.row;
postCell.nameButton.hidden = true;
postCell.noEventNameButton.hidden = false;
}
else{
[postCell.nameButton setTitle:[temp objectForKey:#"User_Name"] forState:UIControlStateNormal];
postCell.nameButton.tag = indexPath.row;
postCell.noEventNameButton.hidden = true;
postCell.nameButton.hidden = false;
//[postCell.eventButton addTarget:self action:#selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];
}
[postCell.eventButton setTitle:[temp objectForKey:#"Event"] forState:UIControlStateNormal];
NSString *createdTime = [NSDateFormatter localizedStringFromDate: temp.createdAt dateStyle: NSDateFormatterNoStyle timeStyle: NSDateFormatterShortStyle];
[postCell.timeButton setTitle:createdTime forState:UIControlStateNormal];
PFFile *imageFile = [temp objectForKey:#"profileImage"];
NSData *data = [imageFile getData];
postCell.profileImage.image = [UIImage imageWithData:data];
NSArray *likeArray = [temp objectForKey:#"likes"];
NSString *likeString = [NSString stringWithFormat:#"%lu Likes", (unsigned long)likeArray.count];
if ([likeArray containsObject:[PFUser currentUser].objectId]) {
postCell.likeButton.hidden = true;
}
else{
postCell.likeButton.hidden = false;
}
NSArray *commentArray = [temp objectForKey:#"Comments"];
NSString *commentString = [NSString stringWithFormat:#"%lu Comments", (unsigned long)commentArray.count];
[postCell.likesButton setTitle:likeString forState:UIControlStateNormal];
[postCell.commentsButton setTitle:commentString forState:UIControlStateNormal];
postCell.eventButton.tag = indexPath.row;
postCell.likeButton.tag = indexPath.row;
postCell.commentsButton.tag = indexPath.row;
postCell.likesButton.tag = indexPath.row;
[postCell.likesButton addTarget:self action:#selector(likesPage:) forControlEvents:UIControlEventTouchUpInside];
[postCell.likeButton addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
[postCell.commentsButton addTarget:self action:#selector(commentsPage:) forControlEvents:UIControlEventTouchUpInside];
[postCell setUserInteractionEnabled:YES];
return postCell;
}
if (self.dataArray == self.personImages) {
UIImage *personImage = [self.dataArray objectAtIndex:indexPath.row];
pictureCell.userImage.image = personImage;
return pictureCell;
}
if (self.dataArray == self.eventsArray) {
[eventCell.userEvent setTitle:[temp objectForKey:#"Title"] forState:UIControlStateNormal];
eventCell.userEvent.tag = indexPath.row;
[eventCell.userEvent addTarget:self action:#selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];
[eventCell.userGroup setTitle:[temp objectForKey:#"Group_Name"] forState:UIControlStateNormal];
eventCell.userGroup.tag = indexPath.row;
[eventCell.userGroup addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
return eventCell;
}
return postCell;
}
The Seg Method:
- (IBAction)segControl:(id)sender {
UISegmentedControl *segment = (UISegmentedControl *) sender;
if (segment.selectedSegmentIndex == 0) {
self.dataArray = self.postsArray;
[self.collectionView reloadData];
}
if (segment.selectedSegmentIndex == 1) {
self.dataArray = self.personImages;
[self.collectionView reloadData];
}
if (segment.selectedSegmentIndex == 2) {
self.dataArray = self.eventsArray;
[self.collectionView reloadData];
}
}
it's late but this will help for others
i also had similar problem , i just replaced [self.myCollectionView reloadData] with [self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]]; to refresh the collectionview and it shows all cells, you can try it.
I have created a drop-down using table cell,where i am showing my data. My data is get display in drop down.Now I want all the cell should be selected initially. Also I want to deselect all cell, and individual cell.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:expansionTableView]) {
forState:UIControlStateNormal];
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
objectForKey:#"surveyName"];
NSMutableArray *list=[[NSMutableArray alloc]init];
NSMutableArray *idlist =[[NSMutableArray alloc]init];
for (int i=0; i<_arraySurveyName.count; i++) {
NSMutableDictionary *dict=[_arraySurveyName objectAtIndex:i];
NSString *surveyName=[dict valueForKey:#"SurveyName"];
NSString *surveyID =[dict valueForKey:#"SurveyId"];
[list addObject:surveyName];
[idlist addObject:surveyID];
}
NSString *item = [list objectAtIndex:indexPath.row-1];
NSNumber *item1= [idlist objectAtIndex:indexPath.row-1];
str = item1;
NSLog(#"%#",str);
[_btn_surveyName setTitle:item forState:UIControlStateNormal];
[expansionTableView setHidden:YES];
}
}
else if ([tableView isEqual:expansionTableViewQtn]){
NSString *selectedQuestion=[arrayOfQuestionDetail objectAtIndex:indexPath.section];
[expansionTableViewQtn setHidden:YES];
[_btn_showQuestn setTitle:selectedQuestion forState:UIControlStateNormal];
}
}
it's my code for "didSelect".How to do this.
You can use below methods to select and deselect the cells
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
[tableView deselectRowAtIndexPath:indexPath animated:NO];//For deselecting
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];//For selecting
I have a bug.
I want to display a UIImageView on cells at special indexPath.row, but these UIImageView repeat while I scroll.
Exemple: I display my UIImageView on a cell indexPath.row == 0, if I scroll down, I see my UIImageView on the cell at indexPath.row == 8.
Here is my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self getCellContentView:CellIdentifier];
}
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UIImageView *imgRead = (UIImageView *)[cell viewWithTag:6];
[cell.contentView insertSubview:imgRead aboveSubview:lblTemp1];
contentDictio = [dict objectAtIndex:indexPath.row];
lblTemp1.text = [contentDictio objectForKey:#"title"];
NSArray *paths_id = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath_id = ([paths_id count] > 0) ? [paths_id objectAtIndex:0] : nil;
NSString *path_id = [basePath_id stringByAppendingPathComponent:#"id.plist"];
NSMutableArray *mut_array_id = [[NSArray arrayWithContentsOfFile:path_id] mutableCopy];
NSMutableDictionary *c0 = [[NSMutableDictionary alloc] init];
NSString *idPlistData = [contentDictio objectForKey:#"id"];
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
}
else {
}
}
}
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect Label1Frame = CGRectMake(kTableCellSmallMargin*2 + 60, kTableCellSmallMargin, 240, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont: [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:15.0]];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
UIImageView *read=[[UIImageView alloc] initWithFrame:CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin, 60, 60)];
read.backgroundColor=[UIColor clearColor];
read.tag = 6;
[cell.contentView addSubview:read];
[read release];
return cell;
}
Thanks...
The cell is cached, so you have to clear it when you want no image, like this:
BOOL found = NO;
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
found = YES;
}
else {
}
}
if (!found)
{
[imgRead setImage:nil];
}