Why tableview cell background view working well after navigation back? - iphone

At this moment i am working on iPhone 5.0 simulator.In my tableview i have taken a uiview for each cell background color and this view has a background image.
Now, My problem is when my table containing controller load then in the- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
i have wrote this for table cell background.
cellView = [[[UIView alloc] initWithFrame:CGRectMake(0,8,250, 51)] autorelease];
cellView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"order_text_area.png"]];
cellView.tag =10;
[cellView.layer setOpaque:NO];
cellView.opaque = NO;
[cell.contentView addSubview:cellView];
For keeping cell background transparent i have used this code and it works,But
[cellView.layer setOpaque:NO];
cellView.opaque = NO;
when i select a cell then it goes it's corresponding controller.Now my problem is when i return back to my tableview controller then cell background do not keep in transparent view means cell view do not work on those code.So anybody help me. I am in serious problem.
NOTE THAT *I wanted to give a screen shot but unexpectedly in simulator i do not face this problem.*
Thanks In Advance.

If you are making a change to the cell background directly in your code, what is probably happening on the device is that when you move to the next screen, the tableView on the previous screen is being unloaded to save memory, and when you go back to it, it is re-loaded and the cells are created again by calling your tableView:cellForRowAtIndexPath: for each cell.
This would also explain why it doesn't happen in the simulator, which is less memeory-constrained and so probably isn't unloading the view. Try simulating a memory warning in the simulator on the second page and then going back to the first view and see if your cell is de-highlighted like it is on the device.
To make the effect persist after the view is reloaded, you will need to remember the cell index that was highlighted before pushing the new view, and then re-highlight the correct cell in your tableView:cellForRowAtIndexPath: method when it is re-created.

do you have
[self.tableView reloadData]
in will appear?
if you already have, try to put a tag at the table view, in the will appear, remove the tag from super view and recreate the table.
hope i understand the question.

Related

Proper way to implement a footer in UITableView

So I read here that I can simply drag a UITableViewCell to the bottom of my UITableViewController in storyboard and have it act like a footer. This footer row has an activity indicator in it that's it. It's width is 320 and height 55 with the indicator centered in the row. Note that it's a UIView rather than a UITableViewCell because I can only get it to work with the former.
First, The UITableView doesn't stop at the footer. One can see the footer if he extends his scrolling beyond the bottom of the UITableView. As soon as the user releases his finger, the footer disappears from site as the UITableView returns its scrolling back to the last element. I am trying to do what Instagram is doing - if you scroll to the bottom you can see their custom activity indicator at the bottom and the UITable will remain its scrolling position at that indicator. How can I achieve this?
Second, I have some custom scrolling performed by certain user actions. Right now, I have the following code:
if (row + 1 < [self.tableView numberOfRowsInSection:0]) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:row+1 inSection:0]
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
} else {
// TODO scroll a little down so the user can see the activityIndicator
}
How can I tell my tableView to scroll programmatically to the footer and have it stop there?
Third During the very beginning, my UITableView has to fetch things from my server before it can populate the tableView. Unfortunately i can't get the footer view to maximize the space of the UITableView so that the activityIndicator will appear in the center. Right now this is how it looks:
I think I have the structs set correctly:
I suspect that having a UIView within a UITableView might prevent the view from maximizing.
Summary
Any recommendations on the above issues? I apologize for the length of this question, but I feel that they are all related to the same problem mentioned above.
I finally figured it out.
With regards to #zing and #Lithu answers, that worked. However, returning a footerView from this method forces the footerView to "stick" to the bottom of the TableView. By this I mean that the footerview will always be shown (which is not something I want)
I finally used self.tableView.footerView = MyView. I disabled ALL the spring and struct settings on my UIView and it fits perfectly. (I've previously set it to maximize hoping to have it maximize when there is nothing in the table)
Regarding centering the activity indicator: I ended up setting the footer view to hidden if there's nothing in the UITableView (by checking in numberOfRows). I programmatically added the ActivityIndicator in the center if this occurs.
Thanks for all the answers!!
Did you try to do it in the proper delegate for UITableview Footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Use This code to add any view in the footer.
e.g.
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 49)];
return footerView;
}

Adding UIPickerView to a UITableViewCell

I want to add a UIPickerView to a UITableViewCell. Right now I am just getting a black window. Can someone also explain me the concept of subclassing UI objects to a cell ie:when do we make our cell as the delegate and the datasource delegate? Thanks
EDIT: Here is the code of what i am doing right now
case 1: {
cell = [tableView dequeueReusableCellWithIdentifier:#"groups"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"groups"];
}
/*NSString *myGroupDetail = [_groupArray objectAtIndex:indexPath.row];
NSLog(#"the group detail label is %#",myGroupDetail);
cell.textLabel.text = myGroupDetail;
*/
[cell addSubview:_groupPicker];
break;
}
The groups is the section in which I want the picker view, I am getting that data from an array.
you would have to allocate and initialize a pickerview in your cellForRow method of the tableviewdelegate. ill sketch it for you =) how to initialize a cell itself should not be hard to find out if you google a bit ;-)
...(tableView *)... cellForRowAtIndexPath... {
if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }
return cell;
}
UPDATE: im currently having trouble with git, so i uploaded a sample project to my private server: UITablePicker example
github: https://github.com/sebamisc/UItableViewWithPicker
yu can modify and use the code however you want =)
sebastian
Well, I never did exactly that.
Does it have to be in a cell or could you use the table's header or footer view for that? (I would not suggest a section header/footer view.)
Assuming it is within a UITableViewCell.
Yes, I personally would subclass UITableViewCell. Did that a lot. In that case you cell object could be the data source delegate of the picker. For doing so your subclass of UITableViewCell (let's assume you name it MyTableViewCell) needs to fulfil the related protocol. You add that UIPickerView programmatically within the init Method (initWithStyle) of MyTableViewCell. For the layout within the table cell, you should overwrite the method layoutSubviews. If your app can be rotated to landscape and portrait orientations and/or if your app is designed to run on iPad as well, then this method should dynamically consider the table's bounds. (Screen or windwo bounds are often used hiere but that is not save when the table is displayed within a split master view or a popup view on iPads.)
Strictly spoken your MyTableViewCell should not be the data source delegate, simply because it is a view element and view objects are not supposed to manage any business logic within an MVC design pattern. Smarter would be the implementation of some dedicated view controller for your table view cell hat fulfills the protocol and is assigned as the delegate. However, both would work. In the event that it is the only picker view within your sell, then you could easily use your UITableViewController subclass even without tagging the UIPickerView.
An alternative to subclassing a UITableViewCell is to create the UIPickerView within the cellForRowAtIndexPath method of your tableViewController. That is fine for singe-orientation apps. However, you may setup it in a way that it re-arranges its the UIPickerView automatically.
In any case you should overwrite the heightForRowAtIndexPath method of UITableViewController when your table views do not have the same hight any more. If all of them still have the same height, then you can simply set the rowHeight property of your UITableView.

Automatic deselection of table view cell after popping detail view?

I've been stumped by this for a while...
When you tap the cell of a contact in Apple's Phone app for the iPhone (entering the view with contact details) and tap back to the previous view, there is a brief animation (fading from blue to white) showing the deselection of the cell. This is the behavior with all table views in Apple's own apps, and also the recommended behavior according to their Human Interface Guidelines.
However, in my own project I've been having trouble replicating this behavior. None of the cells in my UITableView are selected when I return to it from a detail view.
I looked through the CoreDataBooks sample code in Apple's documentation, which has the desired cell deselection behavior, and it seems like the table view gets the behavior "automatically" (without any specific implementation).
I've also tried implementing the solutions in these very similar questions:
What deselect selected cell when navigationController pops a view?
iPhone UITableView cells stay selected
UITableView doesn't keep row selected upon return
But I always get the same result -- none of the cells in the UITableView are selected when I return to it. (Even after adding [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; in the view controller's -viewWillAppear: method.)
It was also suggested in the comments of one question that having [[self tableView] reloadData]; in -viewWillAppear: may be causing the cells not to stay selected. But CoreDataBooks does the same thing and still has the desired behavior (seemingly) without any specific code.
Any suggestions on how to resolve the problem? Thanks in advance.
On a side note, I don't quite understand why the code to deselect the cell should be implemented in -viewWillAppear: (rather than -viewDidAppear:). Wouldn't that cause everything to happen before the table view is displayed on screen? This is probably just due to lack of proper understanding of a view's life cycle on my part, but any clarifications would be nice. Thanks again.
Have you experimented with the clearsSelectionOnViewWillAppear property of a UITableViewController (not just a tableview but a tableviewcontroller)? If you set it to NO it will make the selection stay blue.
To get your desired effect,you will also probably need to add a call to
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
to ensure that it doesn't stay blue forever.
Look in the Overview section of the UITableView Documentation to get started.
//EDIT//
As noted in the comments below, what we finally ended up doing is moving the call to [[self tableview] reloadData]; to viewDidAppear.
My guess is that CoreDataBooks has a complex enough table (with sections and books and sections and books) that something is going on with the timing of events.
If you want to deselect a cell, use the - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated and use [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; or [tableView deselectRowAtIndexPath:indexPath animated:YES];.

UITableCell turning blank after clicked

Im using a regular uitableview with regular uitablecells on it with some text and an image on them. When a cell is clicked I perform tableView: didSelectRowAtIndexPath:, in the end I call [tableView deselectRowAtIndexPath:indexPath animated:YES]; and everything works fine. But in the meantime before the next view loads its contents from the web, the clicked cell turns completely blank. Any clue on that?
If your problem is that you don’t want to show the touched UITableCellView as selected you can try cell.selectionStyle = UITableViewCellSelectionStyleNone;
Maybe if the problem is the load of the new UIView you can use NSThread and detach the process from the main thread.

How can I add a static background behind a UITableView programmatically?

I have a TableViewController and would like to have a static picture as a background, which doesn't scroll along.
The way that everyone recommends using
[UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundPattern.png"]]
doesn't work as it will
1.) move along and
2.) put the background pattern in every cell
I know how to do it in a XIB file (namely adding another layer underneath the TableView) but how do I do it programmatically from the TableViewController?
[myTblViewController.view insertSubview:myImageView belowSubview:myTblViewController.tableView];
That should work.
If it turns out that tableView is not a direct subview of the table view controller's main view, you can try:
[[myTblViewController.tableView superview] insertSubview:myImageView belowSubview:myTblViewController.tableView]; //Edited superview should be all lower case