select custom UITableViewCell by Identifier from xib - objective-c

I try to fill my UITableView withd different UITableViewCells. I tried to choose 1 of 3 UITableViewCells out of a XIB-file by identifier but I can't get the value of the UITableViewCell's Identifier.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:#"CellTwo"];
if(nil == cell){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
if (tmpCell.identifier == #"CellTwo") {
cell = (MyTableViewCell *) currentObject;
}
[tmpCell release];
break;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}

I solved this exact problem by creating a subclass for each UITableViewCell. When you do your comparison of the class type, you can put in the type of cell it is:
cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"footerTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CommonTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
cell = (FooterTableViewCell *)currentObject;
return cell;
}
}
}
I have some tables that reuse the exact same kind of cell formatting and i wanted to have them in a common XIB that i can alter at anytime. In the .h for that controller, i simply declared three sub classes of UITableViewCell for each of the three cell types.

Related

How to add a swipe gesture from left to right and add three buttons in that?

I have a view controller having a table view in which cells are added and i need to crate a swipe gesture with three buttons in that to edit ,move and delete the content of cell.
Here is my code-
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [word count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
WordList *cell;
cell = (WordList *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"WordCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}else{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"WordCell_ipad" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
cell.backgroundColor = [UIColor colorWithRed: 33.0 green: 66.0 blue: 99.0 alpha: 1.0];
cell.word.text = [word objectAtIndex:indexPath.row];
cell.meaning.text = [NSString stringWithFormat:#"meaning: %#",[meaning objectAtIndex:indexPath.row]];
For now, Apple only provide standard "Delete" and "More" button. The reset, you should implement yourself. Here is the great tutorial
Or, you can use third-party solution or another. I've used the former one, but the second looks cool too

Two tableviews in one view doesn't work

I have two UITableviews in my view, delegates and datasources of these two table are connected to File's Owner. I have two IBOutles connected to my UITableView:
IBOutlet UITableView *tableOne;
IBOutlet UITableView *tableTwo;
In my case, the information contained in tableOne are being passed to tableTwo, I believe the problem is inside the method cellForRowAtIndexPath as you can see below:
-(void)viewDidAppear:(BOOL)animated{
[tableOne reloadData];
[tableTwo reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == tableOne) {
return 3;
}else{//Else is the tableTwo
return 3;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == tableOne) {
static NSString *simpleTableIdentifier = #"ContasAlertaTableViewCell";
ContasAlertaTableViewCell *cell = (ContasAlertaTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContasAlertaTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.data.text = [date objectAtIndex:indexPath.row];
return cell;
}else{// Else is tableTwo
static NSString *simpleTableIdentifier = #"FaltasTableViewCell";
FaltasTableViewCell *cell = (FaltasTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FaltasTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.data.text = [date2 objectAtIndex:indexPath.row];
return cell;
}
}
If you notice I'm using different table cells for each of the tables, and I believe that would be what is influencing this error, in my case, What I have to do to solve this type of problem?

Expand / Collapse UITableViewCell with different cell and data source

I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)
here's the part of code where I'm creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"DataTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (DataTableViewCell*)view;
}
}
}
return cell;
}
then when I use the:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method to get the selected cell, I call this method to expand the cell:
- (void)expandCellAtIndex:(int)index {
NSMutableArray *path = [NSMutableArray new];
NSArray *currentCells = [subCellItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++) {
[path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start
how can I set a different cell (with a different xib, and different data) to show instead of the same as before?
basically I have this table working but in the expanding cell I see a copy of the cell you touch
You can do it like this:
In .h:
#interface TSViewController : UIViewController
{
NSMutableArray* subCellIndexPaths;
}
In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.
In .m:
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
if ([subCellIndexPaths containsObject: indexPath])
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSuperCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
else
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSubCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
}
return cell;
}
- (void) expandCellAtIndex: (int)index
{
NSMutableArray* indexPaths = [NSMutableArray new];
NSArray* currentCells = [subCellItems objectAtIndex: index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
inSection: 0];
[indexPaths addObject: indexPath];
[subCellIndexPaths addObject: indexPath];
}
[self.tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
inSection: 0]
atScrollPosition: UITableViewScrollPositionTop
animated: YES];
}
And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).

Is there a way to customize UISearchDisplayController cell

I am using UISearchDisplayController to search and would like to add so more elements to each cell if that is possible. Did some searching but couldn't find any information. The only way is to reproduce it with tableview. Is there any other properties i can set other then textlabel.text
Thanks in advance!
You can change the cell completely.
The following snippet relies on two subclasses of UITableCell, ANormalCell & ASearchCell.
Caveat: This code has not been compiled but you get the gist?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *normalCellReuseIdentifier = #"ANormalCell";
static NSString *searchCellReuseIdentifier = #"ASearchCell";
UITableViewCell* cell = nil;
if (tableView != self.searchDisplayController.searchResultsTableView) {
cell = (ANormalCell*)[self.tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ANormalCell" owner:nil options:nil];
cell = (ANormalCell*)[topLevelObjects objectAtIndex:0];
}
} else {
cell = (ASearchCell*)[self.tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ASearchCell" owner:nil options:nil];
cell = (ASearchCell*)[topLevelObjects objectAtIndex:0];
}
}
return cell;
}
Very possible. All of the tableView datasource methods are called with a tableView parameter. If
if (tableView == searchController.searchResultsTableView)
then alter the cell by adding subviews, or customizing properties. Same approaches apply for custom cells in this case as in any other.
Yes you can set cell.imageView , cell.accessoryView , cell.BackGroundView .. etc.
read this UITableViewCell documentation for further information

Table view doesn't show up custom cells

I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Many thanks
Why are you creating your cell like this?
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If it is your own custom cell why do you use UITableViewCellStyleDefault ?
If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.
In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"nibName" owner:nil options:nil];
for (alrededorCell *view in xib) {
if ([view isKindOfClass:[alrededorCell class]]) {
cell = view;
}
}
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}