Weird UISegmentedControl Problem! - iphone

In my app, one if my goals is to use UISegmentedControl to choose the bg color of another screen. The problem is that I have tried to have it so that whenever you went to the options screen, the Segmented Control will remember the option you picked when you left the screen. It only remembers ONE OUT OF THE 5 OPTIONS!!! Here is the code in the options screen -
- (IBAction)changecolor:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(segcolor.selectedSegmentIndex == 0){
//Red
UIColor *red = [UIColor redColor];
NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:red];
[prefs setObject:colordata forKey:#"ColorKey"];
}else if(segcolor.selectedSegmentIndex == 1){
//Yellow
UIColor *yellow = [UIColor yellowColor];
NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:yellow];
[prefs setObject:colordata forKey:#"ColorKey"];
}else if(segcolor.selectedSegmentIndex == 2){
//Green
UIColor *green = [UIColor greenColor];
NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:green];
[prefs setObject:colordata forKey:#"ColorKey"];
}else if(segcolor.selectedSegmentIndex == 3){
//Blue
UIColor *blue = [UIColor blueColor];
NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:blue];
[prefs setObject:colordata forKey:#"ColorKey"];
}else if(segcolor.selectedSegmentIndex == 4){
//Black
UIColor *black = [UIColor blackColor];
NSData *colordata = [NSKeyedArchiver archivedDataWithRootObject:black];
[prefs setObject:colordata forKey:#"ColorKey"];
}
}
The previous method is connected to the 'value changed' method in IB to the segmentedcontrol, segcolor. And also...
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *colordata = [prefs objectForKey:#"ColorKey"];
if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor redColor])
segcolor.selectedSegmentIndex = 0;
if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor yellowColor])
segcolor.selectedSegmentIndex = 1;
if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor greenColor])
segcolor.selectedSegmentIndex = 2;
if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor blueColor])
segcolor.selectedSegmentIndex = 3;
if ([NSKeyedUnarchiver unarchiveObjectWithData:colordata] == [UIColor blackColor])
segcolor.selectedSegmentIndex = 4;
}
Just for reference, Black is the only option that remembers.
Now for in the main page...
- (void)viewWillAppear:(BOOL)animated {
//---------------------------------------------------------//
//----------------------BGData-----------------------------//
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *colorData = [prefs objectForKey:#"ColorKey"];
UIColor *bgcolor = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
self.view.backgroundColor = bgcolor;
if (bgcolor == [UIColor blackColor]) {
currentArtist.textColor = [UIColor whiteColor];
instructlabel.textColor = [UIColor whiteColor];
currentSong.textColor = [UIColor whiteColor];
} else if (bgcolor != [UIColor blackColor]) {
currentArtist.textColor = [UIColor blackColor];
instructlabel.textColor = [UIColor blackColor];
currentSong.textColor = [UIColor blackColor];
}
}
Who sees what's wrong?

Ouch. You cannot compare contents Objective-C objects with ==. Never. Use -isEqual: to compare UIColors.
And, why don't you directly store the segment index into the user defaults (-setInteger:forKey:, -integerForKey:)? Then you don't need to encode and decode the UIColors which is slow and memory consuming.

Related

Save highlighted State to User Defaults

how can i save the highlighted state of a Image view to user defaults,
and can i save the background Color of a Button to User defaults?(i change the color by click)
to save:
- (IBAction)char1_atheon_cp1_28:(id)sender {
if(_char1_atheon_cp1_28_image.highlighted == NO)
{
_char1_atheon_cp1_28_image.highlighted = YES;
_char1_atheon_cp1_28.backgroundColor =[UIColor greenColor];
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation([UIImage imageNamed:#"check.png"])forKey:#"char1_atheon_28_cp1"];
}
else
{
_char1_atheon_cp1_28_image.highlighted = NO;
_char1_atheon_cp1_28.backgroundColor =[UIColor clearColor];
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation([UIImage imageNamed:#"cross.png"])forKey:#"char1_atheon_28_cp1"];
}
}
to load:
if([[NSUserDefaults standardUserDefaults] objectForKey:#"char1_atheon_28_cp1"] != nil) {
NSData* imageData = [[NSUserDefaults standardUserDefaults]objectForKey:#"char1_atheon_28_cp1"];
UIImage* image = [UIImage imageWithData:imageData];
[_char1_atheon_cp1_28_image setImage:image];
}
Thanks Jürgen

How to create .rtf and edit in iOS?

I have two UIImageView and two textfiled under the image view. One textfield for image1 and second textfield for image2.
Now I want that user enter text and give image name and that image will save in document directory in .rtf format. And the when I open .rtf I will be able to edit that rtf.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,50, 100, 100)];
[imageView setImage:[UIImage imageNamed:#"image1.png"]];
self.view addSubview:imageView];
[imageView release];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100,150, 100, 100)];
[imageView1 setImage:[UIImage imageNamed:#"image2.png"]];
self.view imageView1];
[imageView1 release];
txt=[[UITextField alloc]initWithFrame:CGRectMake(5,190, 180, 30)];
txt.backgroundColor=[UIColor whiteColor];
txt.placeholder=#"Enter Text";
txt.textColor = [UIColor blackColor];
txt.delegate =self;
[txt addTarget:self action:#selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt.textAlignment=NSTextAlignmentCenter;
txt.userInteractionEnabled = YES;
txt.autocapitalizationType = NO;
txt.font=[UIFont fontWithName:#"Helvetica" size:15];
[self.view addSubview:txt];
txt2=[[UITextField alloc]initWithFrame:CGRectMake(200,190, 180, 30)];
txt2.backgroundColor=[UIColor whiteColor];
txt2.placeholder=#"Enter Text";
txt2.textColor = [UIColor blackColor];
txt2.delegate =self;
[txt2 addTarget:self action:#selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt2.textAlignment=NSTextAlignmentCenter;
txt2.userInteractionEnabled = YES;
txt2.autocapitalizationType = NO;
txt2.font=[UIFont fontWithName:#"Helvetica" size:15];
[self.view addSubview:txt2];
I have no idea how to do this.
Any Idea or suggestion would be highly welcome.
you can use the nsuserdefaults to saved the image name
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:_imageName1 forKey:#"imageName1"];
[userDefault setObject: _imageName2 forKey:#"imageName2"];
[userDefault synchronize];
for retrieving the data from nsuserdefaults
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSString *_imageName1 = [userDefault objectForKey:#"imageName1"];
NSString *_imageName2 = [userDefault objectForKey:#"imageName2"];
to save the image in documents directory
create the file path
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath1 = [NSString stringWithFormat:#"%#/imageName1.png",
docDir];
convert the UIImage into nsdata
NSData *data = UIImagePNGRepresentation(myImage.image);
then save the image into imagePath1 of document directory
[data writeToFile:imagePath1 atomically:YES];
for retrieving the image
UIImage *img = [UIImage imageWithContentsOfFile:imagePath1];

Retrieving image from parse.com

I don't know if this is possible but I think it could be possible and I dont know how to do this. I simply want to load an image from parse.com like you retrieve objects from parse.com. Should I do it the same way I get strings from parse.com? I just found how to save images on parse but not how to get them. I would be happy if someone could show me a link to do this or a sample code.
I've already set up a string which is retrieved from parse:
PFQuery *query = [PFQuery queryWithClassName:#"app"];
[query getObjectInBackgroundWithId:#"ID"
block:^(PFObject *textdu, NSError *error) {
if (!error) {
UIFont *welcomeLabelFont = [UIFont boldSystemFontOfSize:17];
welcomeLabel.text = [textdu objectForKey:#"ueberschriftnews"];
welcomeLabel.font = welcomeLabelFont;
welcomeLabel.textColor = [UIColor whiteColor];
welcomeLabel.textAlignment = NSTextAlignmentCenter;
welcomeLabel.backgroundColor = [UIColor clearColor];
welcomeLabel.shadowColor = [UIColor blackColor];
welcomeLabel.shadowOffset = CGSizeMake(0, 1);
[contentView addSubview:welcomeLabel];
// The get request succeeded. Log the score
NSString *text = [textdu objectForKey:#"newstext"];
UIFont *font = nil;
CGFloat points = 17;
CGFloat maxHeight = infoLabel.frame.size.height;
CGFloat textHeight;
do {
font = [UIFont systemFontOfSize:points];
CGSize size = CGSizeMake(infoLabelRect.size.width, 100000);
CGSize textSize = [text sizeWithFont:font constrainedToSize:size lineBreakMode: NSLineBreakByWordWrapping];
textHeight = textSize.height;
points -= 1;
} while (textHeight > maxHeight);
infoLabel.text = text;
infoLabel.numberOfLines = 9;
infoLabel.font = font;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[infoLabel sizeToFit];
[contentView addSubview:infoLabel];
} else {
infoLabel.text = #"something";
[infoLabel sizeToFit];
[contentView addSubview:infoLabel];
}
}];
My parse set up:
Thanks.
Yes, this is possible. If you use web interface to instead of string you should specify PFFile as a type. If you upload image from your iOS client here is a link to parse iOS guide how to do that https://parse.com/docs/ios_guide#files/iOS .
Once your image is there you can download image this way:
PFQuery *query = [PFQuery queryWithClassName:#"app"];
[query getObjectInBackgroundWithId:#"ID"
block:^(PFObject *textdu, NSError *error) {
{
// do your thing with text
if (!error) {
PFFile *imageFile = [textdu objectForKey:#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
}
}];
}
}];

How can I search data from tableview cell?

I have an UITableview controller which representing fetched XML data. For representing these data I used five UILabel. Now I have to add a searchbar at the top of the UITableview. So programmatically I have added a searchbar.
Now I have used searching theorem for search data from the UITableview. But It is not working. I can search data from UItableview when only one text in the UItableviewcell without any UIlabel or something else but in my UItableviewcell cell are taking five UILabel that's why it's becoming tough for me to search data from the UItableviewcell. For understanding I am attaching my code how I am representing my XML data in tableview cell.
This is my XML data representation in UITableviewCell...
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
cell.accessoryType = UITableViewCellSelectionStyleNone;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
cellView.backgroundColor = [UIColor clearColor];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
imgView.image = [UIImage imageNamed:#"productbox.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 2.0;
imgView.tag = 5;
[cellView addSubview:imgView];
CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
idLabel.textAlignment = UITextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.font = [UIFont systemFontOfSize:12];
idLabel.backgroundColor = [UIColor clearColor];
idLabel.layer.borderColor = [UIColor grayColor].CGColor;
idLabel.tag = 0;
CGRect statusRect = CGRectMake(65, 22, 190, 22);
statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont systemFontOfSize:12];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
statusLabel.tag = 1;
CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
orderDate.textAlignment = UITextAlignmentLeft;
orderDate.textColor = [UIColor blackColor];
orderDate.font = [UIFont systemFontOfSize:12];
orderDate.backgroundColor = [UIColor clearColor];
orderDate.layer.borderColor = [UIColor grayColor].CGColor;
orderDate.tag = 2;
CGRect byRect = CGRectMake(65, 75, 190, 22);
byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
byLabel.textAlignment = UITextAlignmentLeft;
byLabel.textColor = [UIColor blackColor];
byLabel.font = [UIFont systemFontOfSize:12];
byLabel.backgroundColor = [UIColor clearColor];
byLabel.layer.borderColor = [UIColor grayColor].CGColor;
byLabel.tag = 3;
CGRect totalRect = CGRectMake(65, 98, 190, 22);
totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
totalLabel.textAlignment = UITextAlignmentLeft;
totalLabel.textColor = [UIColor blackColor];
totalLabel.font = [UIFont systemFontOfSize:12];
totalLabel.backgroundColor = [UIColor clearColor];
totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
totalLabel.tag = 4;
[cellView addSubview:idLabel];
[cellView addSubview:statusLabel];
[cellView addSubview:orderDate];
[cellView addSubview:byLabel];
[cellView addSubview:totalLabel];
}
if(searching == YES){
//[cell setText:[tableData objectAtIndex:indexPath.row]];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
else{
cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
if(pendingOrder == NO && todaysOrder == NO){
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];
}
else if(pendingOrder == YES && todaysOrder == NO){
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:4]];
}
}
return cell;
}
And this searching Delegate.....
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
searching = YES;
// only show the status bar’s cancel button while in edit mode
sBar.showsCancelButton = YES;
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
searching = NO;
sBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:#""] && searchText==nil){
[tableview reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[tableview reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects];
searching = NO;
[tableData addObjectsFromArray:dataSource];
#try{
searching = NO;
[tableview reloadData];
}
#catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = #"";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[sBar resignFirstResponder];
}
For more help to understand i am also attaching my viewDidLoad....
//Add the search bar
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,50)];
sBar.delegate = self;
searching = NO;
[self.view addSubview:sBar];
tableview.dataSource = self;
tableview.delegate = self;
//initialize the two arrays; datasource will be initialized and populated by appDelegate
searchData = [[NSMutableArray alloc] init];
tableData = [[NSMutableArray alloc] init];
[tableData addObjectsFromArray:dataSource];//on launch it should display all the records
Edit
This is my edited portion of numberOfSectionsInTableView and numberOfRowsInSection...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(searching == YES){
searching = NO;
sectionCount = [tableData count];
}
else {
if(pendingOrder == NO && todaysOrder == NO){
sectionCount = [records count];
NSLog(#"section cout: %d",sectionCount);
}
else if(pendingOrder == YES && todaysOrder == NO){
//Total pending order counting
sectionCount = [pendingRecords count];
NSLog(#"section cout for pending: %d",sectionCount);
}
else if(pendingOrder == NO && todaysOrder == YES){
NSLog(#"todays order number counting");
}
}
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
Try this:-
copylistofItem is NSMutableArray copying data that matched with search bar criteria.
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
int i=0
for (NSString *str in [records objectAtIndex:indexPath.i] objectAtIndex:0])
{
[searchArray addObject:str];
i++;
}
for (NSString *sTemp in searchArray)
{
NSString *txtToSearch =[[NSString alloc] initWithString:[sTemp substringWithRange:NSMakeRange(0,[searchText length])]];
if([[txtToSearch lowercaseString] isEqualToString:[searchText lowercaseString]])
{
[copyListOfItems addObject:sTemp];
}
}
[searchArray release];
searchArray = nil;
}
Also we want to know what you have written in your numberOfSections tableView Delegate.

Objective-C: First item of NSArray sometimes is missing

I have and NSArray with 5 string elements, populating an UITableView of my iPhone App.
Everything works fine, but sometimes the first string of my array is missing and I can't figure out why.
Here is my code:
arrayList = [[NSArray alloc] initWithObjects: #"My First String",
#"My Second String",
#"My Third String",
#"My Forth String",
#"My Fifth Sring",
nil];
The element that sometimes just doesn't show up is My First String
Actually I'm using this sample code from Cocoa With Love. Thanks guys for helping me!
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if USE_CUSTOM_DRAWING
const NSInteger TOP_LABEL_TAG = 1001;
// const NSInteger BOTTOM_LABEL_TAG = 1002;
UILabel *topLabel;
// UILabel *bottomLabel;
#endif
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell =
[[[UITableViewCell alloc]
initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier]
autorelease];
#if USE_CUSTOM_DRAWING
UIImage *indicatorImage = [UIImage imageNamed:#"image1.png"];
cell.accessoryView =
[[[UIImageView alloc]
initWithImage:indicatorImage]
autorelease];
const CGFloat LABEL_HEIGHT = 22;
UIImage *image = [UIImage imageNamed:#"image2.png"];
//
// Create the label for the top row of text
//
topLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 1.3 * LABEL_HEIGHT),
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
topLabel.textAlignment = UITextAlignmentCenter;
/*
//
// Create the label for the bottom row of text
//
bottomLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:bottomLabel];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
*/
//
// Create a background image view.
//
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
#endif
}
#if USE_CUSTOM_DRAWING
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
// bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}
topLabel.text = [arrayList objectAtIndex:indexPath.row];
//bottomLabel.text = [NSString stringWithFormat:#"Some other information.", [indexPath row]];
// [cell LabelText:[arrayList objectAtIndex:indexPath.row]];
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected_copy.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:#"topRow.png"];
selectionBackground = [UIImage imageNamed:#"topRowSelected_copy.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"bottomRow.png"];
selectionBackground = [UIImage imageNamed:#"bottomRowSelected_copy.png"];
}
else
{
rowBackground = [UIImage imageNamed:#"middleRow.png"];
selectionBackground = [UIImage imageNamed:#"middleRowSelected_copy.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
if (row == 0)
{
cell.imageView.image = [UIImage imageNamed:#"pic1.png"];
}
else if (row == 1)
{
cell.imageView.image = [UIImage imageNamed:#"pic2.png"];
}
else if (row == 2)
{
cell.imageView.image = [UIImage imageNamed:#"pic3.png"];
}
else if (row == 3)
{
cell.imageView.image = [UIImage imageNamed:#"pic4.png"];
}
else if (row == 4)
{
cell.imageView.image= [UIImage imageNamed:#"pic5.png"];
}
#else
[cell LabelText:[arrayList objectAtIndex:indexPath.row]];
//cell.text = [NSString stringWithFormat:#"Cell at row %ld.", [indexPath row]];
#endif
return cell;
}
The issue you are probably having is accessing object 1 as index 1... Arrays in Objective-C are the same as most other C structured languages. Indexed starting at 0.
Put this code in.
for (int i = 0; i < [arrayList count]; i++)
{
NSLog(#"Item %d = %#", i, [arrayList objectAtIndex:i]);
}
Chances are good that this works fine.
If that is not the case the I would recommend restoring your iphone to factory default as either the operating system is malfunctioning, or you have other rogue processes that are corrupting memory.
To debug the code above. As you are grabbing the items out of the array I would put in an NSLog()
NSString *objectAtIndex = [arrayList objectAtIndex:indexPath.row];
NSLog(#"Row: %d, Object: %#, Count: %d",indexPath.row, objectAtIndex, [arrayList count]);
[cell LabelText:[arrayList objectAtIndex:indexPath.row]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
return cell;
}
Are you doing something similar to this for populating table view
NSMutableArray *ArrayList=[[NSMutableArray alloc]initWithObjects: #"My First String",
#"My Second String",
#"My Third String",
#"My Forth String",
#"My Fifth Sring",
nil];