Make UITableViewCell into a UIView - iphone

I have a UITableView, and in the tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, I want to be able to access the frame that the selected cell is in. Here is what I was trying:
UIView *cell = tableView.indexPathForSelectedRow;
UITableViewCell *cell = (UITableViewCell *)indexPath;
I want to be able to access the view that the selected cell is in.
thanks in advance.

To do this, use:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
than you can access all the info on cell as you would any other UIView
ie. cell.frame, cell.bound, etc.

Accessing the cell directly isn’t necessary—yes, you can get the cell and its frame via -cellForRowAtIndexPath:, but you’d then have to subtract the content offset of the UIScrollView (which UITableView subclasses from) to get the cell’s actual position within the boundaries of the table view. The best approach for this is the UITableView method -rectForRowAtIndexPath:.

Related

Simplest way for UISearchDisplayController to show same cells as the origin

What's the simplest way to make UISearchDisplayController display exactly the same cells (formatting, height, fonts, colors, etc) as the original tableView that it searched on?
or simply put - make it dequeue de same cell identifier?
I am using a the standard subclassed UITableViewController + UISearchDisplayController solution, and prefer to stick to it.
thanks
I actually found the simplest way.
All I had to do is change the default cellForRowAtIndexPath second line of code and add the "self." to tableView to the dequeue cell identifier - this way the the cell style which is in the storyboard always gets dequeued and not the (non-existant) one from the searchResultsController.tableView
Also implement heightForRowAtIndexPath that will return the same height without any check (searchresults table or self.table - we want the same height)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PhraseCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// ^^^^ NEW
//...

Custom CellIdentifier is Null When Using Search Display Controller

In my tableview have custom cells that I initialize from a UITableViewCell class. I have sections for first letters of records and have an indexPath that is being created dynamically.
I wanted to add a search display controller to my tableview. So I did, created all methods to filter data. I am sure that my functions are working well because I am printing array count to screen for search results.
My problem is that the first time view loads, the data is on the screen. But when I hit the search input and type a letter, than I get 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' error. After I used a breakpoint I saw that my custom cell is nil after searching. Data is exist, but cell is not being initialized.
Here is the code I use for custom cell initializing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ObjectCell";
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.label1.text = [myObject objectForKey:#"myValue"];
return cell;
}
I believe I made a mistake when putting controls in IB. So I added screenshots of objects:
Connections inspector for my table view
Connections inspector for my search display controller
EDIT: Problem is actually solved, I have used a UISearchBar instead of Search Display Controller but I guess this issue remains unsolved. So I'm willing to try any ways to make it work.
As of here search display controller question,
you need to access the self.tableView instead of tableView:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CellId"];
// do your thing
return cell;
}
For those using iOS 5 and StoryBoards, you would want to use the following method instead of initWithIdentifier:
initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
Example:
NSString *cellIdentifier = #"ListItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
I'm not sure about how this should work in storeboarding.
But normally you would check if the [tableView dequeueReusableCellWithIdentifier:CellIdentifier] returns a cell.
Because if the cell in not loaded before or there aren't any cells to reuse you will have to create a new cell:
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}
Also when in declaring local variables in Objective-C we tent not to capitalize the first letter.
I had the same issue, with custom cells (built in Storyboard) not being drawn as soon as the first letter was put in the search field. The search was successful however.
Finally I found a good tutorial from Brenna Blackwell suggesting to configure manually the cell drawing in the corresponding subclass of UITableViewCell, adding UILabels and other items.

How to access the cell in tableview?

I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.

how to cascade uitableview's width to its custom table view cell?

I have a certain sized UIView used as a container of a UITableView. I use a subclass of UITableViewCell in my table view.
The problem is that my custom table view cell can not get the proper width from the UITableVIew.
This is how I create the table view
categoryListView = [[UITableView alloc] initWithFrame:categoryListContainer.bounds style:(UITableViewStylePlain)];
This is how I create the custom table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == categoryListView) {
static NSString *CellIdentifier = #"MenuCategoryListIdentifier";
MenuCategoryListCellView *cell = (MenuCategoryListCellView*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// create dynamically
cell = [[MenuCategoryListCellView alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier];
}
....
I wish to be able to get the correct cell bounds to in my MenuCategoryListCellView's initWithStyle method to draw my UI controls. But I don't know how to pass the value down.
Help will be appreciated!
Leo
The system will send the layoutSubviews message to your cell when the cell is resized. Override that method to lay out the UI controls according to the cell's current size.

Data from FirstView

I have no idea how to do this. I have a tableview on firstview with some labels. And they have a disclosure button I want to obtain the data from the labels and show them in secondview in other labels.
How to do it please?
Thank you
To get the selected cell you have to implement:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourCellType *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
}
YourCellType is an UITableViewCell subclass having the desired parameters (UILabel, UIImageView, etc.).
So if you have a UILabel *label1 in cell, you can access it by:
cell.label1
Hope this helps