i got a UITableViewCell which i want to stay selected when clicked on it. So i have this code which works just fine:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [[[self.dataSource objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension]];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor brownColor]];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
Everytime i click on a cell it gets selected with the brown color and stays selected until i click another cell... perfect
But when i dismiss the view by clicking on the back button of my navigationcontroller and then i switch back to my view my cell is not selected anymore. So how can i achieve that the cell which was selected before i switched the views still is selected when i come back to the view ?
I thought i maybe have to create a property from the tableview and then select the row in the viewDidLoad again. The selectedRow index i could save in the nsuserdefaults.. but i hope there is a simpler solution.
In your -viewDidLoad method, add this to the bottom: self.clearsSelectionOnViewWillAppear = NO; Which would make it look something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
Related
I have two different custom cell in my tableview, In the first cell I add a custom cell line under the system cell line:
_bottom_line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, kScreen_Width, 1)];
_bottom_line.backgroundColor = Back_Color_QQ;
[part3 addSubview:_bottom_line];
In the controller to setup my custom cell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) { // my first cell
static NSString *id_cell1 = #"cell1";
AgDetailTechCell *cell = [tableView dequeueReusableCellWithIdentifier:id_cell1];
if (cell == nil) {
cell = [[AgDetailTechCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:id_cell1];
}
((AgDetailTechCell *)cell).model = self.headerModel;
((AgDetailTechCell *)cell).indexPath = indexPath;
((AgDetailTechCell *)cell).delegate = self;
_head_cell = cell;
_head_cell.comemntCountlabel.text = self.headerModel.lml_commentTimes;
_head_cell.likeCountlabel.text = self.headerModel.lml_likeTimes;
return cell;
}else { // other cells
static NSString *id_cell2 = #"cell2";
AgPreOrHelpCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:id_cell2];
if (cell == nil) {
//cell = [[AgPreOrHelpCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:id_cell2];
cell = [[NSBundle mainBundle] loadNibNamed:#"AgPreOrHelpCommentCell" owner:self options:nil].firstObject;
}
cell.delegate = self;
cell.indexPath = indexPath;
[cell initCellDataWithModel:self.dataSource[indexPath.row]];
cell.refresh = ^(UITableViewCell *currentCell) {
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
};
return cell;
}
}
The first time into the controller, it shows well. But after I scroll my tableview to hide and show again, my custom bottom line dismiss, I cannot find it.And I refresh my tableView, the custom line appears again. I have token 2 pictures to explain more detail:
UPDATE
Use Harsh's suggestion, but no change for the issue:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell.reuseIdentifier isEqualToString:#"cell1"]) {
// customs bottom_line
UIView * bottom_line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, kScreen_Width, 1)];
bottom_line.backgroundColor = Back_Color_QQ;
[((AgDetailTechCell *)cell).part3 addSubview:bottom_line];
}
}
If your soul purpose is to extend the default separators from end to end.
Check this post How to fix UITableView separator on iOS 7?
Maybe your bottom line is out of your cell. Make sure you return 50 or greater height for cells. Another thing is disable the system bottom line of UITableView.
I am following a tutorial that uses a custom cell created in Storyboard in a tableview and then adds an accessory view. The table is loading okay. However when I try to add an accessory view (an image), nothing appears. Could there be some setting in storyboard that is wrong? A problem in the code? Or why is the accessory view not appearing
Here is the method where the tutorial says to add the accessory view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
IDContactImport *contactImport = self.contacts[indexPath.row];
IDTableViewCell *idTableCell =(IDTableViewCell *)cell;
idTableCell.contactImport=contactImport;
if (contactImport.imageData==nil)
{
idTableCell.iconView.image = [UIImage imageNamed:#"headshot.jpg"];
}
else{
idTableCell.iconView.image = [UIImage imageWithData:contactImport.imageData];
}
//HERE IS ACCESSORY VIEW
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checkbox.jpg"]];
idTableCell.accessoryView = imageView;
// [self updateAccessoryForTableCell:cell atIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
Thanks for any suggestions
static NSString *cellIdentifier = #"CELLIDENTIFIER";
UITableViewCell *customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
UIImageView *sampleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sampleImage.png"]];
[customCell setAccessoryView:sampleImage];
Hope it useful for you.!!!
It should work. Please try blow code. there is no issue on Storyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"customCell";
CustomCellView *cell = [ctableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
long row = [indexPath row];
cell.name.text = carname[row];
cell.model.text = carModel[row];
cell.carImg.image = [UIImage imageNamed:carImage[row]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedStar_green.png"]];
cell.accessoryView = imageView;
// Configure the cell...
return cell;
}
Change the dequeueReusableCellWithIdentifier: call to:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
This will always returns a cell, so you will never have to check for nil.
// if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
I don't think any of the above answers have addressed your issue. You are right by mentioning that it all has to do with your storyboard.
In order to fix this, go into your Main.storyboard, click on the table view and reset the view to suggested constraints following the below picture.
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've subclassed the UITableViewCell to add custom appearance to it. At the init level of the MYTableViewCell I added 4 subviews: UIImageView, and three UILabel(s). All 4 subviews have a different Tag assigned to them.
Inside the cellForRowAtIndexPath method I either create a new cell if it wasn't available at first, or reuse available one and assign the proper text to the ui labels.
The problem I am having is that if I try to scroll super fast, then the data gets messed up, however if I scroll up and down more slowly, then everything works fine.
Any thoughts??
Below is the code:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"itemListTableViewCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
DisplayableEntity *displayableEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
if( ! cell ) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[self tableView:tableView appearanceForCell:cell withEntity:displayableEntity];
} else {
UIImageView *imageView = (UIImageView *) [cell viewWithTag:IMAGEVIEW_TAG];
imageView.image = [UIImage imageNamed:displayableEntity.displayImageName];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:TITLEVIEW_TAG];
titleLabel.text = displayableEntity.entityName;
UILabel *itemDescription = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG];
itemDescription.text = displayableEntity.entityDesctiption;
}
}
// some code removed to make it brief
- (void)tableView:(UITableView *)tableView appearanceForCell:(MyTableViewCell *)cell withEntity:(DisplayableEntity *)entity {
// cell image view
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[entity displayImageName]]];
[cellImageView setTag:IMAGEVIEW_TAG];
[cell addSubview:cellImageView];
// adding entity name label
UILabel *itemTitleName = [self itemTitleNameLabelWithFrame:itemNameLabelRect itemName:[entity entityName]];
[itemTitleName setTag:TITLEVIEW_TAG];
[cell addSubview:itemTitleName];
// adding 'assigned to' label right under the item name label
UILabel *itemDescriptionLabel = [self itemDescriptionLabelWithFrame:descriptionLabelFrame itemDescription:[entity entityDesctiption]];
[itemDescriptionLabel setTag:DESCRIPTION_TAG];
[cell addSubview:itemDescriptionLabel];
}
I see some troubles in tableView:cellForRowAtIndexPath: logic
It should be:
Dequeue cell
If cell cannot be dequeued - create the new one
Set all cell properties
I mean something like this:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"itemListTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} // <-- Note there is no else, we should reset properties in both cases
NSManagedObject *managedObject = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [managedObject valueForKey:#"text"];
cell.imageView.image = [managedObject valueForKey:#"image"];
return cell;
}
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