I have designed a custom table cell. which displays product information.
When i implement CellForRowAtIndexPath I am doing this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sectionTableCellIdentifier = [[NSString alloc] initWithFormat:#"GLItemTableCellIdentifierNumber%d",indexPath.section];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"GLItemDetailsTableCellIdentifier"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier];
if (cell == nil)
{
NSDictionary *dict = [self.listData objectAtIndex:indexPath.row];
ItemsListTableCell *cell = (ItemsListTableCell *)[tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ItemsListTableCell"
owner:self options:nil];
for (id oneObject in nib)
{
if ([oneObject isKindOfClass:[ItemsListTableCell class]])
{
cell = (ItemsListTableCell *)oneObject;
}
}
NSString *priceinfo = [[NSString alloc] initWithFormat:#"$%#",[dict objectForKey:#"CurrentPrice"]];
NSString *sizeinfo = [[NSString alloc] initWithFormat:#"Size: %#",[dict objectForKey:#"Size"]];
NSString *upcInfo = [[NSString alloc] initWithFormat:#"UPC: %#",[dict objectForKey:#"ID"]];
NSString *strQuantity = [[NSString alloc] initWithFormat:#"%#",[dict objectForKey:#"Quantity"]];
cell.lblProductName.text = [dict objectForKey:#"Name"];
cell.lblSize.text = sizeinfo;
cell.lblBrand.text = [dict objectForKey:#"BrandName"];
cell.lblProductCode.text = upcInfo;
cell.lblQuantity.text = strQuantity;
cell.lblPrice.text = priceinfo;
cell.lblStoreName.text = [dict objectForKey:#"StoreName"];
cell.isSelected = NO;
[cell.btnSelected addTarget:self action:#selector(cellButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[upcInfo release];
[priceinfo release];
[strQuantity release];
[sizeinfo release];
return cell;
}
return cell;
}
now for the click event I am doing
- (IBAction)cellButtonTapped:(id)sender
{
UIView *contentView = [sender superview];
ItemsListTableCell *cell = (ItemsListTableCell *)[contentView superview];
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSUInteger buttonRow = [[self.table
indexPathForCell:cell] row];
NSUInteger buttonSection = [[self.table
indexPathForCell:cell] section];
NSLog(#"Index Path Row : %d",buttonRow);
NSLog(#"Index Path Section : %d",buttonSection);
ItemsListTableCell *buttonCell =
(ItemsListTableCell *)[table cellForRowAtIndexPath:indexPath];
if (buttonCell.isSelected == YES)
{
buttonCell.isSelected = NO;
UIImage *image = [[UIImage imageNamed:#"checkbox-empty.png"] autorelease];
[buttonCell.btnSelected setImage:image forState:UIControlStateNormal];
}
else
{
buttonCell.isSelected = YES;
UIImage *image = [[UIImage imageNamed:#"checkbox-full.png"] autorelease];
[buttonCell.btnSelected setImage:image forState:UIControlStateNormal];
}
self.txtQuantity.text = buttonCell.lblQuantity.text;
NSString *buttonTitle = buttonCell.lblProductName.text;
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You tapped the button"
message:[NSString stringWithFormat:
#"You tapped the button for %#", buttonTitle]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
The Problem is when i click on check button it is going to the event. but I am unable to detect what is the parent cell . as there are some values in cell.
Instead of creating such an event (IBAction), you could do all of these in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
If you want a checkmark of your own style, you could set them up here and finish off things. Makes stuff easier !
Related
I'm working on a project and i stuck at a point. My project has a Table View with some data after clicking on the cell table view will expended into sections. Every thing is fine except one thing i.e i want to set a checkbox image (when the user clicked on a cell).
when i tap on the cell it gives the cell title text but i am not able to set the checkbox image (like UITableViewCellAccessoryCheckmark)on particular cell section.
I am posting the images and the codes below: !
MyTableView it contains the Root Exercise Names
This is the Expended View and i want a checkbox(UITableViewCellAccessoryCheckmark) at the right side of the cell.
When i click on tableView didSelectRowAtIndexPath: it works fine
//code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"]; //from plist
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark- Table View DataSource and Delegates
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
My Question is How to show Checkbox at the selected section?
Sorry! if it is not formatted properly(New to stack)!
Thank You!
I was finally able to get it working. Here's what you have to change:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
_cell.accessoryView = nil;
if ([self.selectedCells containsObject:indexPath])
{
_cell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
And
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"];
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (!thisCell.accessoryView)
{
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
// [alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
If nothing else has changed in those methods, just copy-paste the code and it will work. I tested it before posting it. Let me know if you have more questions
UPDATE
Here is the explanation of what was going on. There were actually a couple of problems:
First, you were mixing accessoryType and accessoryView; this was causing an
incorrect execution of the if-else clause. Here is the old code
// here you are using accessoryType
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
// but here you're using accessoryView
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// here too...
thisCell.accessoryView=nil;
thisCell.accessoryView = nil;
}
Second, you were not saving the indexPaths of the selected elements. Here is the corrected code (note is the same if-else clause):
if (!thisCell.accessoryView)
{
// you have to save the selected indexPaths
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// if the cell was already selected, then remove the indexPath
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
Third, the following line inside of cellForRowAtIndexPath... was messing up things because it was showing another checkmark, so I just removed it:
[_cell.checkUnCheck_ImageView setHidden:NO];
That was pretty much it.
Hope this helps!
You have the right idea of how to do this, however, you're setting the checkmark in the wrong place. You should use the method -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to store (remember) which cell was selected and NOT to set the accessory type.
If only one cell can be selected at a time you can declare aNSIndexPath property to remember the cell that was selected. However, if multiple cells can be selected at a time you can declare a NSMutableArray where you can store all the cells that have been selected.
Then you should use the method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to set the checkmark to the cells that were selected.
Here is an example (I'm assuming multiple cells can be selected):
In your *.h file declare a NSMutableArray property
#property (nonatomic, strong) NSMutableArray *selectedCells;
In your *.m file, inside your init method initialize the property:
_selectedCells = [[NSMutableArray alloc] init];
Then, in the didSelectRowAtIndexPath... method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedCells containsObject:indexPath]) {
[self.selectedCells removeObject:indexPath]; // this line allows the checkmark to be shown/hidden if the user presses several times the same cell
} else {
[self.selectedCells addObject:indexPath]; // a cell was selected; remember it
}
... // the rest of your code
}
Finally, in the cellForRowAtIndexPath... method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... // Create, initialize and customize your cell
// if the cell you're being asked for is saved in the array it means it was selected
if ([self.selectedCells containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
And that's it. Let me know if you have more questions
Hope it helps!
UPDATE
If all you want to do is to set a custom image as your checkmark just create the image and assign it to the accessory view while using the code that was already working for you, like so:
// Note that you should use 'accessoryView' and NOT 'accessoryType'
if (thisCell.accessoryView == UITableViewCellAccessoryNone)
{
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
}
else
{
thisCell.accessoryView = nil;
}
I am new to iphone development. I am developing an app in that i am displaying some contact name with uncheckmark image. Once user taps the button to selected means i am storing it in an nsuserdefaults. Here is the code what i have tried.Actually my problem is another user value will change dynamically.I am adding one boolean value for all the users who are coming in the tableview. If an one more person is added in the another user i want to show in off state. For other users i have to store the values stored in nsuserdefaults. First time i am manually looping 0 in total user.
My interface File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
NSArray * test;
IBOutlet UITableViewCell *customCell;
IBOutlet UIImageView *photo;
IBOutlet UILabel *userName;
NSMutableArray *dataArray;
NSMutableDictionary *tempDictionary;
NSMutableArray * selected;
NSUserDefaults *prefs;
NSMutableArray * statusArray;
NSMutableArray * prefsArray;
}
#property(nonatomic,retain) IBOutlet UITableViewCell *customCell;
#property(nonatomic,retain)IBOutlet UITableView *tblView;
#property(nonatomic,retain) NSMutableArray *dataArray;
#end
implementation file:
- (void)viewDidLoad
{
dataArray=[[NSMutableArray alloc]init];
selected=[[NSMutableArray alloc]init];
prefs=[NSUserDefaults standardUserDefaults];
prefsArray=[[NSMutableArray alloc]init];
NSMutableArray * anotheruser=[NSMutableArray arrayWithObjects:#"da",#"kn",#"gn",#"Prd",#"Kai",#"Sh",nil];
for (int i=0; i<anotheruser.count;i++) {
[selected addObject:[NSNumber numberWithInt:0]];
}
for (int i=0; i< anotheruser.count; i++) {
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[anotheruser objectAtIndex:i] forKey:#"name"];
[dict setValue:[selected objectAtIndex:i] forKey:#"checked"];
[self.dataArray addObject:dict];
}
[super viewDidLoad];
}
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:NSInteger)section
{
return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"MyViewCell" owner:self options:nil];
cell = customCell;
self.customCell = nil;
}
photo.image=[UIImage imageNamed:#"raj.jpg"];
NSMutableDictionary *item = [self.dataArray objectAtIndex:indexPath.row];
userName.text= [item objectForKey:#"name"];
[item setObject:cell forKey:#"cell"];
BOOL checked = [[item objectForKey:#"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"checked.png"] : [UIImage imageNamed:#"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self tableView: tblView accessoryButtonTappedForRowWithIndexPath: indexPath];
[self.tblView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView:tblView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:#"checked"] boolValue];
NSUInteger myInt=indexPath.row;
[item setObject:[NSNumber numberWithBool:!checked] forKey:#"checked"];
UITableViewCell *cell = [item objectForKey:#"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
if (!checked)
{
[selected replaceObjectAtIndex:myInt withObject:[NSNumber numberWithInt:1]];
}else{
[selected replaceObjectAtIndex:myInt withObject:[NSNumber numberWithInt:0]];
}
[prefs setObject:selected forKey:#"status"];
NSLog(#"%#", selected);
UIImage *newImage = (checked) ? [UIImage imageNamed:#"unchecked.png"] : [UIImage imageNamed:#"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
Just try with this code in cellForRowAtIndexPath: method
For CustomCell use like below..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyViewCell *cell = (MyViewCell *) [tableView dequeueReusableCellWithIdentifier:nil];
// MyCustomeCell *cell = (MyCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MyViewCell *) currentObject;
break;
}
}
}
and for DefaultCell use bellow code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = #"MyCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
}
}
I am new for iPhone apps development.
In my app when i clicked on search button ,i want to display record in table view.
i m using array to change record in table view but its not working..please help
viewController.m
-(IBAction) printButtonPressed : (id) sender
{
mylabel.text =keyfileld.text;
UITableView *tableView ;
NSIndexPath *indexPath;
mylabel.text =keyfileld.text;
NSString *post =[NSString stringWithFormat:#"username=%#",keyfileld.text];
NSString *hostStr = #"http://demo.com/search_iphone.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
[states removeObjectForKey:#"1"];
[states removeObjectForKey:#"2"];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [datasource objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if(serverOutput)
{
NSArray *listItems = [serverOutput componentsSeparatedByString:#"#"];
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Congrats" message:#"You are authorized"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
mylabel.text = serverOutput;
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
int x=1;
for (int i=0;i<listItems.count; i++)
{
NSString *myString = [NSString stringWithFormat:#"%d",x];
NSArray *listItemsid = [[listItems objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"place name at index %d: %#\n", i, [listItemsid objectAtIndex:1]);
[states setObject:[listItemsid objectAtIndex:1] forKey:myString];
x++;
[stringArray insertObject:[NSMutableArray arrayWithObjects:str,nil] atIndex:i];
}
datasource = [states allKeys];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Username or Password Incorrect"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
// [alertsuccess release];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
id aKey = [datasource objectAtIndex:indexPath.row];
cell.textLabel.text = [states objectForKey:aKey];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Try calling [tableView reloadData]; after loading the new data into the array's...
when run the following code it give proper result but when go to another view and go back an further search it give the 'mach_msg_trap' error
- (void)viewWillAppear:(BOOL)animated {
AppDeleget= [[UIApplication sharedApplication] delegate];
ProcessView *Process=[[ProcessView alloc] init];
[Process SearchProperty:AppDeleget.PropertyURL page:AppDeleget.Page];
[Process release];
for(NSDictionary *status in AppDeleget.statuses)
{
NSMutableString *pic_string = [[NSMutableString alloc] initWithFormat:#"%#",[status objectForKey:#"picture"]];
if([pic_string isEqualToString:#""])
{
[ListPhotos addObject:#"NA"];
}
else
{
NSString *str= [[[status objectForKey:#"picture"] valueForKey:#"url"] objectAtIndex:0];
[ListPhotos addObject:str];
[str release];
}
}
[NSThread detachNewThreadSelector:#selector(LoadImage) toTarget:self withObject:nil];
[AppDeleget.MyProgressView stopAnimating];
[AppDeleget.Progress removeFromSuperview];
[super viewWillAppear:animated];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// NSString *bedsbaths=[NSString stringWithFormat:#"Beds:%# Baths:%#",[[AppDeleget.statuses valueForKey:#"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:#"baths"] objectAtIndex:indexPath.row]];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:#"mlsno"] objectAtIndex:indexPath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:#"price"] objectAtIndex:indexPath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:#"address"] objectAtIndex:indexPath.row];
// cell.bedsbaths.text=bedsbaths;
cell.bedsbaths.text=[[AppDeleget.statuses valueForKey:#"dispDetails"] objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)LoadImage
{
for(int x=0;x<[ListPhotos count];x++)
{
if ([ListPhotos objectAtIndex:x] == #"NA")
{
UIImage *img = [UIImage imageNamed:#"No_image.png"];
[self performSelectorOnMainThread:#selector(downloadDone:) withObject:img waitUntilDone:NO];
}
else
{
NSData *imageData =[ListPhotos objectAtIndex:x];
id path = imageData;
NSURL *url = [NSURL URLWithString:path];
NSLog(#"%#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:#selector(downloadDone:) withObject:img waitUntilDone:NO];
}
}
}
-(void)downloadDone:(UIImage*)img {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0];
if(img == nil)
{
TableCell *cell = (TableCell *)[TableView cellForRowAtIndexPath:indexPath];
cell.myImageView.image=[UIImage imageNamed:#"No_image.png"];
++count;
[TableView reloadData];
}
else
{
TableCell *cell = (TableCell *)[TableView cellForRowAtIndexPath:indexPath];
cell.myImageView.image=img;
++count;
[TableView reloadData];
}
}
You're releasing objects which were not allocated:
NSString *str= [[[status objectForKey:#"picture"] valueForKey:#"url"] objectAtIndex:0];
[ListPhotos addObject:str];
[str release];
Here, 'str' shouldn't be released, because it's an autoreleased object.
HI,
how to implement checkmark, one at a time in a uitableview.and then how to save that state in my application? please guide.
right now i am having a table and the data in table row is coming from a nsmutablearray.My .m file is as:
#import "LocationSelection.h"
#implementation LocationSelection
#synthesize menuList, table;
- (void)viewDidLoad {
menuList=[[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"LOCATION1",nil],
[NSArray arrayWithObjects:#"LOCATION2",nil],
[NSArray arrayWithObjects:#"LOCATION3",nil],
[NSArray arrayWithObjects:#"LOCATION4",nil],
[NSArray arrayWithObjects:#"LOCATION5",nil],
[NSArray arrayWithObjects:#"LOCATION6",nil],
[NSArray arrayWithObjects:#"LOCATION7",nil],
nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
self.title=#"Location Selection";
[table reloadData];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{
return menuList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted=NO;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSArray *rowArray = [menuList objectAtIndex:indexPath.row];
UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)]autorelease];
nameLabel.text = [NSString stringWithFormat:#"%#",[rowArray objectAtIndex:0]];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.shadowColor=[UIColor whiteColor];
nameLabel.shadowOffset=CGSizeMake(0.0, 0.5);
nameLabel.textColor = RGB(0,0,0);
[nameLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[cell.contentView addSubview:nameLabel];
return cell;
}
Thanks!
Comment this line in cellForRowAtIndexPath -> cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Use this:
NSIndexPath* lastIndexPath; // This as an ivar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
}
In your MenuList you should stock NSDictionaries having 2 keys:
location
visited (if the location should have the checkmark)
NSDictionary *loc = [[NSDictionary alloc] initWithObjectsAndKeys
#"Location 1", #"location",
#"NO", #"visited", nil, nil];
When setting up the cell you would test to see if the "visited" key has a value:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:#"visited"]){
// the location was visited
// setup the checkmark
}
Lastly, to fill in the name of the location, instead of:
nameLabel.text = [NSString stringWithFormat:#"%#",[rowArray objectAtIndex:0]];
put
nameLabel.text = [[rowArray objectAtIndex:0] valueForKey:#"location"];
Hope this helps
Use
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Instead of
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
in your code ...