I have a problem with a table view.
When I selected row, my height of cell is changing and display some information. If I select a second time, it's closing.
Works like a charm on simulator, but on real device, first part work fine, but when I want close it, he is executing the code but probably not refreshing tableView, because my information stay displayed.
Another problem for real device: my views is not appear.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==1 &&show == YES) {
return 50 + descLabel;
}else if (indexPath.row == 2 && show2 == YES){
return caracLabel;
}
{
return 50;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellID = #"detailCell";
switch (indexPath.row) {
case 0:
cellID = #"detailCell1";
break;
case 1:
cellID = #"detailCell2";
break;
case 2:
cellID = #"detailCell3";
break;
case 3:
cellID = #"detailCell4";
default:
break;
}
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
if (indexPath.row == 1) {
if (show == NO) {
cell.labelDef.hidden =YES;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==1) {
if (show == NO ) {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell2" forIndexPath:indexPath];
show=YES;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.labelDef.text=[self.desc objectAtIndex:pos];
cell.labelDef.sizeToFit;
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_5#3x.png"];
cell.labelDef.frame = CGRectMake(10, 50, cell.labelDef.frame.size.width, cell.labelDef.frame.size.height);
cell.labelDef.hidden = NO;
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 50, cell.frame.size.width, cell.labelDef.frame.size.height)];
[view setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:view];
[cell.contentView addSubview:cell.labelDef];
descLabel = cell.labelDef.frame.size.height ;
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}else{
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell2" forIndexPath:indexPath];
show= NO;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_2#3x.png"];
cell.labelDef.hidden = YES;
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}else if (indexPath.row == 2) {
if ( show2 == NO) {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell3"forIndexPath:indexPath];
show2=YES;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_5#3x.png"];
NSArray *firstWords = [[self.carac objectAtIndex:pos] componentsSeparatedByString:#"\n"];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, cell.frame.size.width , 100)];
[view1 setBackgroundColor:[UIColor colorWithRed:246/256.0 green:245/256.0 blue:241/256.0 alpha:1]];
[cell.contentView addSubview:view1];
caracLabel = 50;
for (int i = 0; i <firstWords.count; i++) {
NSArray *array = [[firstWords objectAtIndex:i]componentsSeparatedByString:#"/"];
UILabel *label = [[UILabel alloc]initWithFrame: CGRectMake(10, caracLabel, 90, 20)];
label.text = [array objectAtIndex:0];
label.textColor = [UIColor colorWithRed:150/256.0 green:150/256.0 blue:150/256.0 alpha:1];
label.numberOfLines = 0;
[label sizeToFit];
[cell.contentView addSubview:label];
UILabel *label1 = [[UILabel alloc]initWithFrame: CGRectMake(110, caracLabel, 205, 20)];
label1.text = [array objectAtIndex:1];
label1.textColor = [UIColor colorWithRed:150/256.0 green:150/256.0 blue:150/256.0 alpha:1];
label1.numberOfLines = 0;
[label1 sizeToFit];
caracLabel = caracLabel + label1.frame.size.height;
if (i != firstWords.count - 1) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, caracLabel, cell.frame.size.width, 1)];
[view setBackgroundColor:[UIColor whiteColor]];
caracLabel =caracLabel+2;
[cell.contentView addSubview:view];
}
[cell.contentView addSubview:label1];
}
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 1, caracLabel - 50)];
[view setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:view];
view1.frame = CGRectMake(0, 50, cell.frame.size.width, caracLabel-50);
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell3"forIndexPath:indexPath];
show2= NO;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_2#3x.png"];
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}else
if (indexPath.row == 3){
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.wiki objectAtIndex:pos]]];
UIWebView *video = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - +50)];
[video loadRequest:request];
video.delegate=self;
showWeb = YES;
UIActivityIndicatorView *actInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
actInd.color=[UIColor blackColor];
[actInd setCenter:self.view.center];
self.activityIndicator=actInd;
[self.view addSubview:video];
[video addSubview:self.activityIndicator];
}
}
This is how looking/work on simulator :
closed-
description-
caracteristics-
And this is how works for real device:
description-
caracteristics-
closed-
and
I don't know if that is problem, but row is changing colour after i press first time in real device. Why? Any ideas?
If everything working well in simulator but not in Device then check "dequeueReusableCellWithIdentifier"/"CellID" values because simulator is not case sensitive, but device is case sensitive.
It might be not find out your cell ID so it happen, I'm not sure about it but just check once.
Related
I'm creating a UISwitch programmatically in one of the tableview datasource function(cell for row at index). when I'm scrolling the table view, the OFF state switches are malfunctioned(UI) two rounds appeared. Attaching the screenshot of the switch.
Appreciate your help!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60, (cell.contentView.frame.size.height/2)-20 , 100, 100)];
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
[cell.contentView addSubview:myswitch];
myswitch.tag = indexPath.row;
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have faced the same issue and fixed with following code before creating switch
for(UIView *subView in cell. contentView.subviews){
if ([subView isKindOfClass:[UISwitch class]]) {
[subView removeFromSuperview];
}
}
Or
static NSString *TableIdentifier = #"YourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
//place your all UI elements code here.
}
I have updated your code, try this will work for you,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
if(mySwitch == nil){
myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
[cell.contentView addSubview:myswitch];
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
}
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
myswitch.tag = indexPath.row+597; //597 is extra number added to tag
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I am trying to create a collapse table like this
My code :
- (WordListTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"wordListTableCell";
WordListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[WordListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *uu = [wordList objectAtIndex:indexPath.row];
if(!addingNewCellFlag)
{
if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"en"])
{
cell.textLabel.text = [uu valueForKey:#"tiitle_en"];
}
else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"ru"])
{
cell.textLabel.text = [uu valueForKey:#"tiitle_ru"];
}
UIImage *buttonImage;
if(indexPath.row==0)
{
buttonImage = [[UIImage imageNamed:#"rowFirst.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
}
else
{
buttonImage = [UIImage imageNamed:#"rowMiddle.png"];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.isCanExpanded = TRUE;
}
else
{
if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"en"]){
cell.textLabel.text = [uu valueForKey:#"content_en"];
}
else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"ru"])
{
cell.textLabel.text = [uu valueForKey:#"content_ru"];
}
cell.backgroundColor = [UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.textAlignment = NSTextAlignmentJustified;
cell.isCanExpanded = FALSE;
}
[cell setIsExpanded:FALSE];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WordListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
addingNewCellFlag = TRUE;
if(cell.isCanExpanded)
{
if(![cell isExpanded]){
[cell setIsExpanded:TRUE];
NSDictionary *uu = [wordList objectAtIndex:indexPath.row];
[wordList insertObject:uu atIndex:indexPath.row+1];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]
withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
else if ([cell isExpanded])
{
[cell setIsExpanded:FALSE];
[wordList removeObjectAtIndex:indexPath.row+1];
[self.tableView beginUpdates];
NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
}
cell = nil;
}
It works, if we click on cell we create disruption cell, is the new cell has right text ([uu valueForKey:#"content_en"]), but have wrong backgroundColor (cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];). But sometimes the backgroundColor is right ([UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1]).
Please, tell me, how does this method works.
I don't see your code in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Maybe it's wrong when you deal with the cells of different indexpaths.
I have two sections in my tableview(iOS), where the first one is for the "favorites" rows. If you select the accessoryView it should add/remove the selected object in the "objects" array to/from "favs" array depending if the object already exist in the section.
The method I wrote almost works, but when I add a second row, or the same to remove it in favorites, the app crashes with this error:
Terminating app due to uncaught exception NSRangeException', reason: *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]
The problem is that the index of the object in the corresponding arrays is not the same so I can't really figure out how to make this work properly.
Here's some code:
-(void)addToFavs:(id)sender{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
//if favorite section is empty
if ([self.favs isEqualToArray:[#[]mutableCopy]]) {
NSLog(#"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
//if selected row already exist in favorites array <--HERE IS THE PROBLEM (I THINK)
else if([[self.objects objectAtIndex:gesture.view.tag] isEqualToString:[self.favs objectAtIndex:gesture.view.tag]]){
NSLog(#"removing favorite");
[self.favs removeObjectAtIndex:gesture.view.tag];
[self.subtitlesFavs removeObjectAtIndex:gesture.view.tag];
[self.iconsFavs removeObjectAtIndex:gesture.view.tag];
}else{
NSLog(#"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}
//fav image
UIImageView *fav = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"favorites.png"]];
[fav setFrame:CGRectMake(0, 0, 25, 25)];
[fav setClipsToBounds:YES];
if(!indexPath.section==0) {
fav.image=[UIImage imageNamed:#"unfavorites"];
}
//cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
cell.accessoryView = fav;
//Favorites
cell.accessoryView.userInteractionEnabled = YES;
cell.accessoryView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addToFavs:)];
tapped.numberOfTapsRequired = 1;
[cell.accessoryView addGestureRecognizer:tapped];
//favorites section contents
if (indexPath.section == 0)
{
cell.textLabel.text=self.favs[indexPath.row];
[cell.contentView addSubview:image];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}
}
return cell;
}
Update 1
I edited my code with "isEqualToString" in the if-condition but it still doesn't work to remove them...
Quite similar to what you have but try this anyways:
this be the -cellForRowAtIndexPath: method --
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
//following commented line not needed
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// left image
UIImageView *image = [[UIImageView alloc] init];
[image setFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
[image.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[image.layer setBorderWidth:0.3f];
[cell.contentView addSubview:image];
//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 25, 25)];
[button setBackgroundColor:[UIColor clearColor]];
[button setImage:[UIImage imageNamed:#"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateSelected];
[button setTag:100]; //tag needed later
[button addTarget:self action:#selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
[cell setAccessoryView:button];
switch(indexPath.section) {
case 0: { //is first section (favourite)
image.image = [UIImage imageNamed: self.iconsFavs[indexPath.row]];
cell.textLabel.text = self.favs[indexPath.row];
cell.detailTextLabel.text = self.subtitlesFavs[indexPath.row];
[button setSelected:YES];
}
break;
case 1: { //is second section (all clubs)
image.image = [UIImage imageNamed: self.icons[indexPath.row]];
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = self.subtitles[indexPath.row];
//change state of button (thereby change button image)
if([self.favs containsObject: self.objects[indexPath.row]]) {
[button setSelected:YES];
} else {
[button setSelected:NO];
}
}
break;
}
//separator -- do the following on viewDidLoad instead
//[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
return cell;
}
this be the -addToFavs:event: method --
//No need for `-accessoryButtonTappedForRowWithIndexPath:` method
//it can be done in your following `-addToFavs:event:` method alone:
-(void)addToFavs:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
switch(indexPath.section) {
case 0: { //is first section (favourite)
//remove from favourites
//get indexPath for appropraite row in second section
NSUInteger i_indexOfFavInMain = [self.objects indexOfObject:self.favs [indexPath.row]];
NSIndexPath *indexOfFavInMain = [NSIndexPath indexPathForItem:i_indexOfFavInMain inSection:1];
[self.favs removeObjectAtIndex:indexPath.row];
[self.subtitlesFavs removeObjectAtIndex:indexPath.row];
[self.iconsFavs removeObjectAtIndex:indexPath.row];
//handle second section button (for the appropriate row)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexOfFavInMain];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];
[btnTemp setSelected:NO];
//reload first section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
case 1: { //is second section (all clubs)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];
if([self.favs containsObject:self.objects[indexPath.row]]) {
//remove from favourites
[self.favs removeObject:self.objects[indexPath.row]];
[self.subtitlesFavs removeObject:self.subtitles[indexPath.row]];
[self.iconsFavs removeObject:self.icons[indexPath.row]];
[btnTemp setSelected:NO];
} else {
//add to favourites
[self.favs addObject: self.objects[indexPath.row]];
[self.subtitlesFavs addObject: self.subtitles[indexPath.row]];
[self.iconsFavs addObject: self.icons[indexPath.row]];
[btnTemp setSelected:YES];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
}
}
Here's the solution. Moving everything to accessoryButoonTappedForRowWithIndexPath and having four different cases did the trick. Also make sure to not have strings that are the same in the arrays otherwise it will remove all of them and you will get an index error.
-(void)addToFavs:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if(indexPath!=nil){
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if ([self.favs count]==0) {
NSLog(#"adding first favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs addObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs addObject:[self.icons objectAtIndex:indexPath.row]];
}else if (indexPath.section==0){
NSLog(#"removing favorite %#",[self.favs objectAtIndex:indexPath.row]);
[self.favs removeObjectAtIndex:indexPath.row];
[self.subtitlesFavs removeObjectAtIndex:indexPath.row];
[self.iconsFavs removeObjectAtIndex:indexPath.row];
}else if(indexPath.section==1 && [self.favs containsObject:[self.objects objectAtIndex:indexPath.row]]){
NSLog(#"removing favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs removeObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs removeObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs removeObject:[self.icons objectAtIndex:indexPath.row]];
}else{
NSLog(#"adding favorite %#",[self.objects objectAtIndex:indexPath.row]);
[self.favs addObject:[self.objects objectAtIndex:indexPath.row]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:indexPath.row]];
[self.iconsFavs addObject:[self.icons objectAtIndex:indexPath.row]];
}
[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
[image.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[image.layer setBorderWidth:0.3f];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor=[UIColor lightGrayColor];
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
cell.textLabel.text=self.favs[indexPath.row];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}
//separetor
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 25, 25);
button.frame = frame;
button.showsTouchWhenHighlighted = YES;
[button setImage:[UIImage imageNamed:#"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
return cell;
}
I am trying to make quiz program. I used to UITableview for answers. How can i make to flashing on background when user click the correct answer? Is it possible?
Here is the some part of my project;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
//cell=[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum] )
{
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *text = [answers objectAtIndex:indexPath.row];
NSData *data = [text dataUsingEncoding: [NSString defaultCStringEncoding] ];
cell.textLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ];
//cell.textLabel.text=[answers objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
tableView.backgroundColor=[UIColor whiteColor];
cell.backgroundColor=[UIColor whiteColor];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvatica" size:15]];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
CGRect rect = cell.frame;
UIView * view = [[UIView alloc] init];
UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ListGray"] ];
BOOL selx;
if (answerType==2)
selx = [self isSelected:indexPath];
else
selx = [lastIndexPath isEqual:indexPath];
if (selx) {
back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"aaaa.png"] ];
cell.textLabel.textColor = [UIColor whiteColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
} else {
cell.accessoryView = nil;
if (lastIndexPath != nil){
cell.textLabel.textColor = [UIColor blackColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
back = nil;
}
}
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
tableView.allowsSelection = NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if (answerType==3) {
NSString *secilenAnswerID;
secilenAnswerID= [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
for (int a=0; a<[answers count]; a++) {
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:a inSection:0]];
cell. accessoryType= UITableViewCellAccessoryNone;
[selectedCells removeObject:rowNsNum];
selectedCells=[[NSMutableArray alloc]init];
}
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
isRadioSelected=TRUE;
radioAnswer = [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
[selectedCells addObject:rowNsNum];
lastIndexPath = indexPath;
[tableView reloadData];
}
}
Sorry for my bad english. Thank you for your interest.
You can add a background view into your UITableViewCell with a backgroundColor. Then in your tableView: didSelectRowAtIndexPath: you can animate the hidden or alpha with the animation APIs in UIView. When the animation finishes you can animate hidden to TRUE or alpha back to 0 to get the flash effect.
Here is my question's solution. I took advice from Stephen Johnson:)
[UIView animateWithDuration:0.1f delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[UIView setAnimationRepeatCount:3];
cell.alpha = 0;
} completion: ^(BOOL finished) {
cell.hidden = YES;
[UIView animateWithDuration:0.1 animations:^{
cell.alpha = 1;
} completion: ^(BOOL finished) {
cell.hidden = NO;
}];
}];
I need to get indexPath of cell to delete but my app has auto reload after 2s to check new file so indexpath has changed so I don't get it correctly. Now, I want get cell but I don't know how to store cell. This is my codes:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//[_tableView clearsContextBeforeDrawing];
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.tag = indexPath.row;
if([FileNameArray count] != 0){
CGRect frame = CGRectMake(10, 05, 100, 20);//(20, 0, 80, 40);
UILabel *lbl1 = [[UILabel alloc]initWithFrame:frame];
[lbl1 setFont:[UIFont fontWithName:#"Helvetica" size:15.0]];
[lbl1 setTextColor:[UIColor blackColor]];
[lbl1 setTextAlignment:UITextAlignmentLeft];
lbl1.text = [FileNameArray objectAtIndex:indexPath.row];
[cell addSubview:lbl1];
[lbl1 release];
}
return cell;
}
removeCell function:
-(void) removeCell:(NSIndexPath *)indexPath
{
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
// [_tableView reloadData];
[_tableView endUpdates];
}
Do you have suggestions?