I have a behavior issue in UITableView,
that behavior issue on youtube.
My code here:
- (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];
}
NSString *fileName = (NSString *)_movies[indexPath.row];
UIImage *thumbnail = [UIImage imageWithContentsOfFile:_movieThumbnails[indexPath.row]];
[cell.textLabel setText:fileName];
[cell.imageView setImage:thumbnail];
[cell.imageView setClipsToBounds:YES];
[cell.imageView setAutoresizingMask:UIViewAutoresizingNone];
return cell;
}
The first thumbnail resolution is 640*360, second is 160*90,
Cell height is 60.
Test device is iPhone 5 iOS 7.1.2, and iPhone 5s iOS 7.0.6,
Those has same behavior issue.
tableView:willDisplayCell:forRowAtIndexPath: on here:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.imageView setFrame:CGRectMake(15.0f, 0.0f, 106.0f, 60.0f)];
}
This frame size is current imageView frame by log.
Ok so the reason for this behavior is that tapping delete button launches animation which changes frame of the content view and this results in this weird animation.
Solution is to create a subclass of UITableViewCell and in layoutSubviews method change the frame of the imageView and label to fit contentView properly (assuming that size of the image is always the same). This way you would end up with something like this in your TableViewDatasource:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *fileName = (NSString *)_movies[indexPath.row];
UIImage *thumbnail = [UIImage imageWithContentsOfFile:_movieThumbnails[indexPath.row]];
[cell.textLabel setText:fileName];
[cell.imageView setImage:thumbnail];
}
Also if you're targeting iOS 6 and higher you can use registerClass:forCellReuseIdentifier: in UITableView and simplify the code above a bit (remove boilerplate code with allocating table view cell).
Related
I developed iPhone app in which i have UITableView.
UITableViewCell textlabel frame is not proper according to text,
when i write this code, UITableView textlabel is not proper.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:12.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [tempArr objectAtIndex:indexPath.row];
return cell;
}
when i pass my array to UITableView it shows like this:
Whats the problem ? where i am doing mistake, please help
I am not sure what text your array contains. But, I have created an example of my own. Here is the code :
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor = [UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:20.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = #"Brand";
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor redColor];
}
#end
Here is the output :
I have used your code. Only difference I made is, I am applying red colour to text label in willDisplayCell: method of UITableViewDelegate.
Coming to the margins that appears on both sides of the tableview are as per Apple's UI guidelines.
Let me know if you need my code.
Try to set Size to fit with content After setting Text.
[cell.textLabel sizeToFit];
Thanks.
Check table row height property in size inspector, may be some thing wrong with it
[tableView setRowHeight: 100.00];
or you can use delegate to set row height
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Using UITableView and found that cells are not dequeuing properly.
Here is the code which i have written.I have read that, have to override prepareForReuse, but how to use that method, not getting. Please help me out.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Here is the image of UITableView which i am getting when i scroll the TableView
I am not sure if this will fix your issue, but this code uses a local UICell * variable, uses self.tableLabelArray rather than accessing the property iVar directly and uses the textLabel property rather than searching by tag.
My suspicion is that your cell variable is an iVar, not a local so that may be causing you problems, and therefore this may help, but it could also be that you need a font size smaller than 16, so I have specified adjustsFontSizeToFitWidth=YES
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *returnCell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (returnCell==nil) {
returnCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
returnCell.textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
returnCell.adjustsFontSizeToFitWidth=YES;
returnCell.text = [self.tableLabelArray objectAtIndex:indexPath.row];
return returnCell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
return cell;
}
How to draw image on the right side of the UITableView cell on iOS 7 on iPad?
Tried and don't help the following code:
tableView.contentInset = UIEdgeInsetsZero;
[cell contentView].frame = CGRectMake(0, [cell contentView].frame.origin.y, cell.frame.size.width, [cell contentView].frame.size.height);
You most likely want to set the accessoryView property on UITableViewCell. This will put a an image view on the right side of a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Configure cell
}
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"<some image name>.png"]];
cell.accessoryView = image;
}
In my project I have tableViews with static cells as well as tableViews with dynamic cells. In order to customized I've managed to get a gradient background on the cells (grouped sytle).
It works ok with dynamic TableViews as I set the background view in cellForRowAtIndex... according to the position of the row (Top, Bottom, Middle or single).
However, when I try to implement it on the static tableview cells, it doesn't work. I've tried to implement the cellForRowAtindex... but it crashes.
Does someone have an idea?
Update: the code for cellForRow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init];
if (indexPath.row==0) {
bgw.position = UACellBackgroundViewPositionTop;
cell.backgroundView=bgw;
}else if (indexPath.row==2){
bgw.position = UACellBackgroundViewPositionBottom;
cell.backgroundView=bgw;
}else {
bgw.position = UACellBackgroundViewPositionMiddle;
cell.backgroundView=bgw;
}
// cell.backgroundView=bgw;
return cell;
}
By the way, the Background view I got it from here: http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds and here: http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
if somebody is interested
It doesn't look like you are alloc the UITablViewCell, you need to alloc the cell.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Cell Name";
cell.detailTextLabel.text = #"Cell Detail";
return cell;
}
Add this statement:
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If you have a UITableViewController subclass with a static table you should not try to dequeue cells.
Instead you should ask super for the cell. The superclass will get the cell from the storyboard and you can configure it.
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
UIView *selectedBackgroundView = [[UIView alloc] init];
cell.selectedBackgroundView = selectedBackgroundView;
cell.selectedBackgroundView.backgroundColor = [UIColor mb_tableViewSelectionColor];
return cell;
}
Works for all other attributes as well.
I would like to know if it is possible to assign some kind of icon/image to the navigation table cells on the left side of the UISplitViewController in an iPad App.
e.g. an image next to row 1...
http://www.cre8ive.kr/blog/wp-content/uploads/SplitViewTest_03.png
I am trying to approach this with editing the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
method.
Here's what I got
// 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"%i", indexPath.row +1];
//Let the magic happen here :D
UIImage *image = [UIImage imageNamed:#"deviantart_dark.png"];
//Nothing changes :(
[[cell imageView] setImage:image];
return cell;
}
Any ideas?
You could use the below code,
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage:imageNamed:#"deviantart_dark.png"]];