This is how my tableviewcell is setup
else if(indexPath.row == 3)
{
cell.textLabel.text = #"Category";
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 2, 145, 34)];
categoryLabel.adjustsFontSizeToFitWidth = YES;
categoryLabel.textColor = [UIColor blackColor];
categoryLabel.font = [UIFont systemFontOfSize:17.0];
categoryLabel.text = #"select a category";
categoryLabel.backgroundColor = [UIColor clearColor];
categoryLabel.textAlignment = UITextAlignmentRight;
categoryLabel.tag = 3;
[cell addSubview:categoryLabel];
[categoryLabel release];
}
I need to change the the text of the category label later on in the program.
How do I accomplish this? I assume I need to use the tag to reference the UILabel?
This is how my tableviewcell is setup
else if(indexPath.row == 3)
{
cell.textLabel.text = #"Category";
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 2, 145, 34)];
categoryLabel.adjustsFontSizeToFitWidth = YES;
categoryLabel.textColor = [UIColor blackColor];
categoryLabel.font = [UIFont systemFontOfSize:17.0];
categoryLabel.text = #"select a category";
categoryLabel.backgroundColor = [UIColor clearColor];
categoryLabel.textAlignment = UITextAlignmentRight;
categoryLabel.tag = 3;
[cell addSubview:categoryLabel];
[categoryLabel release];
}
I need to change the the text of the category label later on in the program. How do I accomplish this? I assume I need to use the tag to reference the UILabel?
========================
I figured it out...
I put the UILabel in my header file and changed the code like so
else if(indexPath.row == 3)
{
cell.textLabel.text = #"Category";
categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 2, 145, 34)];
categoryLabel.adjustsFontSizeToFitWidth = YES;
categoryLabel.textColor = [UIColor blackColor];
categoryLabel.font = [UIFont systemFontOfSize:17.0];
categoryLabel.text = [NSString stringWithFormat:#"%#",categoryFriendlyString];
categoryLabel.backgroundColor = [UIColor clearColor];
categoryLabel.textAlignment = UITextAlignmentRight;
categoryLabel.tag = 3;
[cell addSubview:categoryLabel];
}
Then when I wanted to change it later I did this
categoryLabel.text = #"sample string";
And finally released it in my dealloc method.
Related
I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.
Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
cellText.font = [UIFont boldSystemFontOfSize:11.0];
cellText.textColor = [UIColor grayColor];
[cell.contentView addSubview:cellText];
if (indexPath.section == 0)
{
cellText.text = clientInfoArray[indexPath.row];
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Customer Name"])
{
nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
nameTxtField.placeholder = #"full name";
nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:nameTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Phone"])
{
phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
phoneTxtField.placeholder = #"xxx-xxx-xxx";
phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
[phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:phoneTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Date"])
{
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:dateLabel];
dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
}
}
if (indexPath.section == 1) {
cellText.text = vehicleInfoArray[indexPath.row];
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Make and Model"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:modelLabel];
modelLabel.text = makeAndModelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Color"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:colorLabel];
colorLabel.text = colorData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Doors"])
{
doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
doorsTxtField.placeholder = #"#";
doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:doorsTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Vehicle VIN"])
{
vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
vinTxtField.placeholder = #"#";
vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:vinTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Fuel"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:fuelLabel];
fuelLabel.text = fuelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Milage"])
{
milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
milageLabel.placeholder = #"ODO";
milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
[milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
milageLabel.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:milageLabel];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Other description"])
{
otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
[cell addSubview:otherDTxtView];
}
}
if (indexPath.section == 2) {
cellText.text = InAndOutInfoArray[indexPath.row];
}
return cell;
}
Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)
Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.
I have a UITextField in a UITableViewCell.
Even though I set -
textField.userInteractionEnabled = NO;
textField.enabled = NO
But when I click on the table cell which contains the textField, the keyboard comes up for the textfield.
Why is this happening and how can I prevent it?
EDIT: Strangely this is happening when I first set some text in the textfield. When the textfield is empty, it is not editable.
EDIT: Code for cellForRowAtIndexPath -
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;
if (_editingNotAllowed) {
[textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
[textField setUserInteractionEnabled:NO];
textField.enabled = NO;
} else {
[textField setUserInteractionEnabled:YES];
}
[cell.contentView addSubview:textField];
You should create an UITableViewCell as shown in this repo :)
https://github.com/breeno/EditingUITableView
And use your custom UITableViewCell like this:
if(condition){ // Check the row must be TextField
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(!cell){
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.label.text = #"Title Row"; //UITextLabel Title
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
cell.textField.returnKeyType = UIReturnKeyDone;
cell.textField.backgroundColor = [UIColor clearColor];
cell.textField.textAlignment = NSTextAlignmentLeft;
cell.textField.tag = 1;
} else { // Normal UITableViewCell
}
im making a tableview with checkboxes. I did implement the checkboxes with UIButtons and i can check and uncheck them without problems. The problem came up when i tried to make a "select/unselect all" button and this is the resultant code:
-(IBAction)select:(id)sender{
if (all==YES) {
all=NO;
}
else {
all=YES;
}
[tblPeticiones reloadData];
}
The problem is that the table doesn't reload the data.
Any idea?
Thanks and regards.
EDIT:
I load de data like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Datos
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *fInicio = [dateFormatter stringFromDate:[(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] DEP_DATE]];
NSString *loc = [(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] LOCATION];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:#"EUR"];
[formatter setLocale:[NSLocale currentLocale]];
NSString *precio = [formatter stringFromNumber:aux];
//Vista
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
CustomTVC *cell = (CustomTVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[CustomTVC alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
//CheckBox
UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[checkButton setFrame:CGRectMake(10, 10, 23, 23)];
if (todos==NO) {
[checkButton setBackgroundImage:[[UIImage imageNamed:#"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
checkButton.tag = 0;
}
else {
[checkButton setBackgroundImage:[[UIImage imageNamed:#"checkSI.png"] str etchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
checkButton.tag = 1;
}
[checkButton addTarget:self action:#selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkButton];
[cell.contentView addSubview:checkButton];
//fecha
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 70.0,tableView.rowHeight)] autorelease];
[cell addColumn:0];
label.tag = 1;
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = fInicio;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
//label.backgroundColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
if (indexPath.row % 2 == 0){
label.backgroundColor = [UIColor colorWithRed:233.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0];
} else {
label.backgroundColor = [UIColor clearColor];
}
//localizaciĆ³n
label = [[[UILabel alloc] initWithFrame:CGRectMake(115, 0, 75.0,tableView.rowHeight)] autorelease];
[cell addColumn:180];
label.tag = 2;
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = loc;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
//label.backgroundColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
if (indexPath.row % 2 == 0){
label.backgroundColor = [UIColor colorWithRed:233.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0];
} else {
label.backgroundColor = [UIColor clearColor];
}
}
return cell;
}
Debug the problem
Check datasource methods of tableView whether they are being called or not after reload?
You should store which rows are checked in an array. Then check this array when you load / reload the tableview (in cellForRowAtIndex) to see if the row should be checked.
Then if you want to select all or none, just delete or add them to the array and reload the tableview.
If you need help implementing this, let me know.
i found the problem. This is the working code:
-(IBAction)select:(id)sender{
if (all==YES) {
all=NO;
[btnSelAll setBackgroundImage:[[UIImage imageNamed:#"checkSI.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}else {
all=YES;
[btnSelAll setBackgroundImage:[[UIImage imageNamed:#"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}
NSMutableArray *arrIndex = [NSMutableArray new];
for(int i=0;i<[m count];i++) {
[arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tblPeticiones beginUpdates];
[tblPeticiones reloadRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
[self.tblPeticiones endUpdates];
[tblPeticiones reloadData];
}
Some comments about your code:
There seems to be global variable called all, whose value you toggle, when button is pressed. However inside cellForRowAtIndexPath you check value of another global variable called todos. Is there some connection between these variables?
You change checkbox background image only, when you create a new cell. Since cells are recycled, you need to move this code outside if (cell == nil) i.e. afterwards, where you are customizing a recycled cell
You should only check whether if (todos) and if (!todos), don't compare to values like YES or NO. Also easiest way to toggle a boolean is todos = !todos :)
I am trying to implement an image in a UIImagePicker that has only one component. I add an uimageview and a uilabel. The catch is that the first row should NOT have an image as well as the 3rd row which I believe I have done right. My problem is that the images for each row are different so to solve this I have put them into an array of images and then based on what row is being passed in, I display the image. However; it is not working for me and seems to display the same image over and over. Can anyone tell me why? Thanks in advance!
imageNames= [[NSArray alloc] initWithObjects:#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
/
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *imageName= [imageNames objectAtIndex:theRow];
NSLog (#"the image name is %#", imageName);
NSLog (#"the current count is %d", theRow);
if(component == 0)
{
if (row == 0 || row == 17)
{
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
return tmpView;
}
else {
UIImage *img = [UIImage imageNamed:imageName];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(120, 15,70, 27);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
[tmpView insertSubview:temp atIndex: 1];
return tmpView;
}
}
}
EDITED CODE
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *imageName= [imageNames objectAtIndex:row]; // This is where the issue is I believe. What do I use to go through the whole image array?
NSLog (#"the image name is %#", imageName);
NSLog (#"the current count is %d", row);
if(component == 0)
{
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIImage *img = [UIImage imageNamed:imageName];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(120, 15,70, 27);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
[tmpView insertSubview:temp atIndex: 1];
return tmpView;
}
}
Your problem is "theRow" - its not even defined in this method. You should just put a placeholder string in the array - that is at position 0 and 17 just put #"". Now you can use "row" to index into your array and all will be well.
EDIT: So your code implies you have 18 rows, want to show an image in most rows, but do something different for rows 0 and 17. Thus, you need an array of size 18.
imageNames= [[NSArray alloc] initWithObjects:
#"", // row 0 unused
// 1 - 6
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
// 7 - 12
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
// 13 - 16
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",
// 17
#"" // not used
// keep adding
nil];
The concept is to insure that you always have the same number of objects in the array as you have rows.
You can add an assert at the start of this method:
assert(row < [imageNames count]);
which will cause an exception if what you believe to be true turns out not to be.