I have a cellForRowAtIndexPath method that uses a switch to build cells like so:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"Usar mi ubicacion actual.jpg"];
[cell.imageView setFrame:CGRectMake(5, 5, cell.imageView.image.size.width, cell.imageView.image.size.height)];
break;
but its not working. I changed it to this:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
switch (indexPath.row) {
case 0:
self.locationButtonImageView.image = [UIImage imageNamed:#"Usar mi ubicacion actual.jpg"];
//cell.imageView.image = [UIImage imageNamed:#"Usar mi ubicacion actual.jpg"];
break;
The cell loads without the button image. If I tap the cell, the toggle works and displays the selected button image and from then on it keeps toggling. So it may be an issue of loading too late, right?
UITableViewCell default style can't be overridden by position.
You need to customize the cell and add your own imageView to its contentView or subclass using xib file and add your imageView to it
Related
I'm attempting to put an instance of UIDatePicker in the accessory view of a UITableView cell, and have been following this SO thread as a template. However, it looks as if the picker is being placed above the cell entirely:
Below is the code I'm using to try to add a Date Picker to the accessory view of a UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellNewRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
switch (indexPath.row) {
case EmployeeOvertimeRow:
cell.textLabel.text = NSLocalizedString(#"Test", #"One");
_datePicker = [[UIDatePicker alloc]init];
_datePicker.datePickerMode = UIDatePickerModeTime;
//OLD: cell.accessoryView = _datePicker;
//POST EDIT
[cell.contentView addSubview:_datePicker];
break;
default:
break;
}
return cell;
}
Does anyone have any guidance on what I'm doing wrong or how to fix this?
The accessoryView of a UITableViewCell is surely not what you think : it's the little view at the right of the cell, typically, it's an arrow, and it's pretty small, not made to be the width of the screen at all. You should try adding your view to the contentView. You will need to set a bigger height for your cell in the heightForRowAtIndexPath method, too.
I am working a UITableViewController with a dynamic TableView, which displays some information about a dish. Everything is working fine, but one cell just stays blank.
When I debug the code, all things look like expected (tableView:cellForRowAtIndexPath: gets called for each portion in self.menuItem.menuPortions, portion isn't nil and values of it's properties are valid, the UILabels aren't nil either), but the first portionCell stays empty, no matter how much portions self.menuItem.menuPortions holds...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
switch (indexPath.section) {
case 0:{//image
cell = [tableView dequeueReusableCellWithIdentifier:#"imageCell" forIndexPath:indexPath];
UIImageView* imageView = ((UIImageView*)[cell viewWithTag:1]);
[self.menuItem setImageOfImageView:imageView OrDeleteCellAtIndexPath:indexPath fromTable:tableView];
break;}
case 1:{//description
cell = [tableView dequeueReusableCellWithIdentifier:#"descriptionCell" forIndexPath:indexPath];
UITextView * descriptionTextView = (UITextView*)[cell viewWithTag:1];
descriptionTextView.text = self.menuItem.menuItemDescription;
break;}
case 2:{//portions
cell = [tableView dequeueReusableCellWithIdentifier:#"portionCell" forIndexPath:indexPath];
id<MenuItemPortionProtocol> portion = self.menuItem.menuPortions[indexPath.row];
UILabel * sizeLabel = (UILabel*)[cell viewWithTag:1];
sizeLabel.text = portion.size;
UILabel * priceLabel = (UILabel*)[cell viewWithTag:2];
priceLabel.text = [portion priceString];
break;}
case 3:{//categories
cell = [tableView dequeueReusableCellWithIdentifier:#"categoriesCell" forIndexPath:indexPath];
UILabel* categoriesLabel = (UILabel*)[cell viewWithTag:1];
categoriesLabel.text = [self.menuItem.categories componentsJoinedByString:#", "];
break;}
default:
break;
}
// Configure the cell...
return cell;
}
Does someone have a hint, what goes wrong?
I use this code to create an image inside a cell:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier"] autorelease];
switch (indexPath.row) {
...
case 5:
cell.imageView.image = [UIImage imageNamed:#"Search.png"];
break;
default:
break;
}
return cell;
}
How do I center the image in the cell? Do I HAVE to subclass UITableViewCell?
You can do this if you don't use the provided cell.imageView but instead add a new UIImageView to the cell. Try something like this at the appropriate place in cellForRowAtIndexPath:
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Search.png"]];
newImageView.center = cell.center;
[cell addSubview:newImageView];
Better and efficient way to do this is using Custom UITableViewCell. So in feature, you can easily customize the TableViewCell.
You can do this through both code and Interface builder.
Custom UITableViewCell Using Interface Builder
Customize Table View Cells for UITableView
cell.imageView.center = cell.center;
I created an option menu using a UITableViewController and can get the labels to set correctly, but not the images.
I tried taking out the switch statement and using an array filled with the correct values based on [indexPath row] and could not get it to work correctly either.
Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
switch ([indexPath row]) {
case 0:
[[cell imageView] setImage:[UIImage imageNamed:#"42-photos.png"]];
[[cell textLabel] setText:#"Photos"];
break;
case 1:
[[cell imageView] setImage:[UIImage imageNamed:#"280-clapboard.png"]];
[[cell textLabel] setText:#"Videos"];
break;
case 2:
[[cell imageView] setImage:[UIImage imageNamed:#"42-info.png"]];
[[cell textLabel] setText:#"About"];
break;
default:
return cell;
break;
}
return cell;
}
The UITableViewCellStyleValue2 doesn't include the image:
A style for a cell with a label on the left side of the cell with text
that is right-aligned and blue; on the right side of the cell is
another label with smaller text that is left-aligned and black. The
Phone/Contacts application uses cells in this style.
You should use UITableViewCellStyleDefault instead (or design your own cell).
You have not created a custom cell i guess , it's better to have a custom cell if you have so many controls in your UITableViewCell and then accessing the property to the controls you can change the image.you can refer this post of mine.Your style chosen will not support image.
I am trying to build a UITableViewCell that looks like this:
Since I can't post an image yet I'll try to describe it by saying it's a label (left) with a UISwitch (middle) and the accessory (right).
Hope ya'll get the picture...
The idea is that the accessoryView is visible but disabled if the switch is off. When the user turns on the switch then they can tap and navigate right to see the list of options that they can select. Trouble is, when the switch is tapped, the cell gets the tap not the switch.
What I gotta' do? (to make the switch get the tap first). I'm guessing it's a firstResponder thing but I'm not finding the magic code that I need.
Once I get past this I can probably figure out the enable/disable of the accessory my self...
Thanks.
Create UISwitch control and add it to the cell content view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = (UITableViewCellAccessoryNone;
UISwitch* aSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
aSwitch.tag = [indexPath row];
CGRect rect = cell.frame;
CGRect switchRect = aSwitch.frame;
switchRect.origin = CGPointMake( (rect.size.width / 2) - (aSwitch.frame.size.width / 2),
(rect.size.height / 2) - (aSwitch.frame.size.height / 2));
aSwitch.frame = switchRect;
[aSwitch addTarget:self action:#selector(switchSwitched) forControlEvents:UIControlEventValueChanged];
[cell addSubview:aSwitch];
[aSwitch release];
}
return cell;
}
- (void)switchSwitched:(UISwitch*)sender {
if (sender.on) {
UITableViewCell* aCell = [self.tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:sender.tag inSection:0]];
aCell.accessoryType = (sender.on == YES ) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
}
}
You can also implement this differently by Subclassing UITableViewCell and adding UITableViewCell nib file.
Make the UIViewTableController the file owner of the Cell nib file, add to the UIViewController a IBOutlet for the subclassed cell. Load the custom cell using
[[NSBundle mainBundle] loadNibNamed:#"Your Custom Cell nib file name" owner:self options:nil];
see Apple programming guide for iOS http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7