UICollectionView multiple cells selected at same time - iphone

I have a UICollectionView and a UICollectionViewCell as subclass, in this cell i have buttons and labels created from XiB, when i click any UIButton in cell i'm changing the BG image of UILabel in that cell, then i Scroll the UICollectionViewCell i see same thing was happened of some other cell. How can i solve this???
This is how i'm calling custom class..
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = #"Cell";
NMCFAIPadWishListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell setWishList:[[self wishListData] objectAtIndex:[indexPath row]] delegate:self];
return cell;
}

Try this code Tell me its working or not
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NMCFAIPadWishListCell *cell = (NMCFAIPadWishListCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell.imageCategory.image:[UIImage imageNamed:#"selectedeimagename.png"]
];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NMCFAIPadWishListCell *cell = (NMCFAIPadWishListCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell.imageCategory.image:[UIImage imageNamed:#"deselectedeimagename.png"]
];
}

Related

Programmatically adding a UITableView - how do I set the reuse identifier for the cells?

I have a UIViewController that at some point grows a UITableView, and when it does I simply init the TableView instance variable and add it to the view, but I'm not sure how to handle the dequeueing of cells to add to the view; I need a reuse identifier, but I'm not sure how to set it.
What do I do within this method?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"wot";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
Use the method initWithStyle:reuseIdentifier
check if cell exists
If it doesn't, then you need to initialize it.
code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath
{
static NSString *cellIdentifier = #"wot";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier];
return cell;
}
The reuse identifier need not be explicitly defined.In the cellForRowAtIndexPath method the definitions you included in question is enough to work with
for Reference
Eg
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]];
}
Region *region = [regions objectAtIndex:indexPath.section];
TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneWrapper.localeName;
return cell;
}

Change an image when UITableViewCell is selected

I have a UITableViewController, In that I added a UIImageView to those cells. This UIImageView is in a custom TableViewCell. So my question is how to change this image when i select the row using didSelectRowAtIndexPath delegate method. I tried, it is showing image on tableviewcell but after clicking this image is not changing. How to resolve this?
here is my code -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MantrasListCell *customCell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (customCell == nil)
{
customCell = [[MantrasListCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
customCell.imgView.image = [UIImage imageNamed:#"play.png"];
return customCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MantrasListCell *customCell = (MantrasListCell*)[tableView
cellForRowAtIndexPath:indexPath];
customCell.imgView.image = [UIImage imageNamed:#"pause.png"];
}
This link will explain you. All you need to do is rather than hidden you can set image accordingly.
How to check cell is selected in UItableview for image display

Changing the image of the cell when it's checked

In my UITableView, i need to get the event of the user, so when the cell is selected(checked), i put btn_checked.png and when it's unchecked, i put the btn_unchecked.png.
I guess, what i try to do should be in the didSelectRowAtIndexPath method, so this is my code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_unchecked.png"]];
theCell.accessoryView = imageView;
}
My snippet code doesn't work, so the btw_unchecked.png isn't placed when the row is checked, also, i need to get the event of checked/unchecked to place the right image to the right event.
Thanx in advance.
As an example, this code snippet will toggle the cell's checkmark when the cell is tapped. The pattern is to force a selective reload from the table's datasource when the delegate method is invoked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue the cell... then:
static NSString *CellIdentifier = #"tvcOEList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
You can use following code.
[cell.contentView addSubview:imageView];
i hope this will help you..

Fixed height for a UITableViewCell

I have a UITableView, and i want each cell to be a fixed height at 50px, how can this be done in Xcode4?
Heres my tableview, loaded from XIB file
- (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];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}

How to get cell text based on indexPath?

I have a UITabBarController with more than 5 UITabBarItems so the moreNavigationController is available.
In my UITabBarController Delegate I do the following:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...
UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
moreView.delegate = self;
}
I want to implement a UITableViewDelegate so I can capture the row that was selected, set a custom view property and then push the view controller:
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
}
I need to get the text of a cell when the user taps on a row. How can I accomplish this?
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
}
A better Solution is to maintain Array of cell and use it directly here
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
cell.textLabel.text = service.name;
cell.detailTextLabel.text = service.description;
if(![self.mutArray containsObject:cell])
[self.mutArray insertObject:cell atIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
NSString *str = cell.textLabel.text;
}
Swift 5
You can always get your cell from UITableVIew or UICollectionView based on IndexPath like this:
tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)