I have a UIView that gets added to my UITableView.view as a subview. It looks great and works fine, but when I scroll on my UITableView, the subview gets chopped up and disappears but only part of it (mostly the part that scrolls off). I can get it to come back on by calling:
[self.view bringSubviewToFront:self.myView];
but I'd rather not have to call this whenever the UITableView scrolls. Is there something I'm doing wrong? Thanks in advance.
Try this ..
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
}
It's hard to tell what you end goal is but adding a subview to UITableView is kind of counter intuitive. The UITableView manages it's subviews (UITableViewCell's) and they are probably taking precedence over your subview. Are you looking for backgroundView?
Related
When the user taps a UITableViewCell I want an image in that cell to make a little bouncing animation to provide user feedback. That animation works exactly like I want except it is not always triggered when I'm tapping a cell. I'm using Facebook POP animation framework.
#import <POP/POP.h>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"customcell" forIndexPath:indexPath];
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1,1)];
scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(5,5)];
scaleAnimation.springBounciness = 20.f;
[cell.bounceimage pop_addAnimation:scaleAnimation forKey:#"scaleAnimation"];
}
Do I need to reset the animation each time a cell is tapped, or anything else I'm missing?
Thanks in advance
The problem is that you are creating (or re-using) a cell every time didSelectRowAtIndexPath is called because you are using dequeueReusableCellWithIdentifier. Instead the first line of you implementation should be:
MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath: indexPath];
Probably a simple question, but I can't find the answer.
When I set my textLabel it overwrites my Detail Disclosure button that I have in my content view of the cell, put in through the storyboard. Now it only shows when the cell is selected. My code looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"accountCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
User *user = [arr objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [user username];
return cell;
}
When I take away cell.textLabel.text = [user username]; it shows all the time, as I want.
Any tips? Guess I gotta put some code in for my button...
Figured it out...
To understand this question, you need to understand that a UITableViewCell only has three spaces - imageView, textLabel and accessory. When you set the textLabel it overwrites everything you've created in the content area in storyboard. I had to set a a new class, with subclass UITableViewCell and set the custom IBOutlets in that one, then link in the custom class to my TableView.
So I'm trying to add a UITableView on the lower half of my ipad app which will be used to display a search result. This is how I did it.
I added a UIView
I added a UItableView onto the UIView
I then dragged the UITableView to the ViewController so it can connect to it for delegate and datasource.
This is what it currently looks like:
(It's at that middle top row)
So I added the following onto the viewcontroller class to generate the data
# pragma mark TableView properties
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SearchResultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
}
The debugger would go through all these but would get stuck after the "cellForRowAtIndexPath" method:
It would just go through that and would not end until I stop the whole debugging. Not really sure what's going on.
Thoughts? Maybe you guys can point me to the right direction as to how I should generate my search results.
Thanks!
I usually find it much more faster and easier to use the free Sensible TableView framework to do automatic table view searches, instead of using the regular datasource/delegate system which I could never get right.
I initiated a UITableView with the following code:
ProductTableView *tableProd = [[ProductTableView alloc]initWithNibName:#"ProductTableView" bundle:nil];
the xib file does exist!
Since I am displaying this table in a separate UIView I add it to this screen by:
[content addSubview:tableProd.view];
I used xcode to create a standard UITableView and set the following functions:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (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];
}
cell.textLabel.text = #"test";
return cell;
}
The table is displayed in the simulator with 10 rows filled with test. However, when I start scrolling, and the first cell leaves the screen, the simulator crashes with a EXC_BAD_ACCESS error. I tried using Instruments to detect the NSZombie and the software flaged the zombie. Unfortunately I cant trace this error back to the cause.
Does anyone have a idea what is going wrong here?
if you're adding the view of a view controller as a subview of another view controllers view, i would guess that the ProductTableView view controller is being dealloced (since adding as a subview retains the view, but not the view controller it belongs to)
add the ProductTableView view controller as a property of the container view controller so that it is retained
I have a UITableView where I set the height to be larger than default (using heightForRowAtIndexPath. The image in the cell is always 32x32 and us currently being vertically aligned to center. I'd like that the image will be at the top of the cell. I tried something like this (setting the bounds explicitly):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier] autorelease];
cell.imageView.bounds = CGRectMake(7,7,32,32);
....
}
....
}
but this doesn't work. Any simple way to make it without creating a complete custom cell?
Take a look at this article here
You can do some pretty custom modifications if you play with the content view, you just need to know how to do it ;)