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;
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'm having a custom cell that I create like this:
CategoryCellTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"poiCell"];
if (!cell)
{
cell = [[CategoryCellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"poiCell"];
}
// Get current POI
Rank * poi = [_data objectAtIndex:indexPath.row];
cell.text.text = poi.name;
cell.categoryImageView.image = [UIImage imageNamed:#"animals.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map"]];
cell.accessoryView = imageView;
return cell;
The accessoryView shows up. But when I click on it, it doesn't come in my:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Accessory clicked");
}
Do I need to set some delegate right? I thought this was already implemented for you so you could always use it?
You need your own handler for you custom view. This delegate method called only for default accessory button.
For example you may add custom UIButton with image as accessory view and add target and action to this button.
-Hi, I have a problem with a tableview reloading data and what I want to do is that you put the selected cell in blue and change an image that is, the problem is that if I make the [self.mytableview reloadData] in the didselectedRow blue background disappears and if I do the image of the cell does not change, I'm a bit lost with this piece of code I give thanks
if (indexPath.row == _selectedRow) {
UIImageView *favView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"favIconSelected.png"]];
CGRect frame = favView.frame;
frame.origin.x = 294;
frame.origin.y = 7;
favView.frame = frame;
[cell.contentView addSubview:favView];
[favView release];
}
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:SELCETED_BGIMGCELL]]autorelease];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Product *p = [_productList objectAtIndex:indexPath.row];
_selectedRow=indexPath.row;
[_delegate productWasSelected:p];
[self.myTableView reloadData];
}
You don't need to call reloadData at all. All you need to do is to update the cell that was selected.
In the didSelectRowAtIndexPath method you can get the cell directly by calling cellForRowAtIndexPath on the tableView. This will return the actual cell that's on display and you can directly set the image it's displaying.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = ...;
Alternatively you could control the image display from your cell subclass if you provide it with both the normal and selected images when you configure each instance.
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
I am having trouble removing the white border that appears between each cell inside my UITableView. I have tried using:
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
inside my viewDidLoad method, but unfortunately that does not seem to work. I am using a background image for my cells to display, and it is the border between these images that I wish to remove. The image itself is the size of each cell, and the size of each cell I set in my CustomTableViewCell class as follows:
self.contentView.frame = CGRectMake(0, 0, 320, 80);
My cellForRowAtIndexPath method is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"mycell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.ID.text = [[DataModel sharedInstance].ID objectAtIndex:indexPath.row];
cell.Name.text = [[DataModel sharedInstance].Name objectAtIndex:indexPath.row];
cell.Date.text = [[DataModel sharedInstance].Date objectAtIndex:indexPath.row];
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cell.png"]]];
return cell;
}
Despite everything I have done, I am seeing a clear white border between my cells in the table. I really would like to remove this. Can anyone see what I am doing wrong?
Inside Interface Builder - change the separator to None and then the separator color to clear color:
Setting UITableView separatorColor at viewDidLoad might be too early. Try viewWillAppear instead.