resizableImageWithCapInsets won't strech Image for UITableViewCell Background - iphone

I'm customising the background of a UITableViewCell. I use the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
UIImageView* uiv= [[UIImageView alloc]initWithImage:[[UIImage imageNamed:#"bkgCell44.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)]];
uiv.frame = CGRectMake(0, 0, 320, 44);
cell.backgroundView = uiv;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
The image is displayed, but it's not streched at all...
Any help to point at what I'm doing wrong ?
Same code used to work fine for buttons and Nav bars...

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIImageView* uiv= [[UIImageView alloc]init];
uiv.image = [UIImage imageNamed:#"bkgCell44.png"];
uiv.frame = CGRectMake(0, 0, 320, 44);
uiv.contentMode = UIViewContentModeScaleAspectFit;
cell.backgroundView = uiv;
cell.backgroundColor = [UIColor clearColor];
}

Related

TableviewCell change unexpectablly

I have created an app that contain tableview now i am setting background of that cell and add a view for separator. it look fine but when tebleview scroll than my separator disappear. like this,
First time
When table scroll
this is my code for adding tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
}
return cell;
}
USE
cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
Try
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, 320, 1)];
[separatorLineView setTag:1];
[cell.contentView addSubview:separatorLineView];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
}
UIView *SPview=[cell viewWithTag:1];
SPview.backgroundColor = [UIColor blueColor];// you can also put image here
return cell;
}
The problem is the frame you set for the view I changed it from 44 to 42 for the seperator line orgin and now it works fine
Move the configuring of the cell outside of
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
}
// everything else
should do the trick.
Use it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
return cell;
}
Your problem is you're not configuring your cell each time you get a new one. You should populate your cell each time cellForRowAtIndexPath is called.
Try changing your code to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
return cell;
}
Good answer at here.
First add separator thru the xib file and than put this code in cellForRowAtIndexPath.
tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];

Enhance UITableView Scrolling

in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(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];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;

uitableview - data and if (!cell)

I have a big problem with an UITableView, I want to use a label inside the cell, so I use this method for do it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
}
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
return cell;
}
in this way I can see all the data on my table, but the label are overlap, than I try to use this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
}
else {
}
return cell;
}
the label are OK but in this case I can see on my table only 5 data, and this 5 data are repeat for some time...
For example if in the first case on my table I can see: 1,2,3,4,5,6,7,8,9,10,...
in the second case I seee: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...
where is the problem?
add this code
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
before
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
Currently you add a label to the cell and the next time the cell is reused..the label is still there and you add a label on top of it.
The code you posted is specifying UITableViewCellStyleSubtitle as the cell style, which means each cell will a text label and detail text label available in its corresponding properties textLabel, and detailTextLabel. So there's no reason for you to allocate additional instances of UILabel. Instead, just populate the text properties of the existing labels. For example, you could rewrite your implementation like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
cell.textLabel.text = (indexPath.section == 0 ?
[array objectAtIndex:indexPath.row] :
#"ReadMore");
return cell;
}

overlapping subviews in UITableViewCell

I am stuck with this. I will explain with my code
- (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];
}
NSNumber *cellno=[NSNumber numberWithUnsignedInteger:indexPath.row];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
switch (indexPath.row) {
case 0:
cell.textLabel.text=#"apples";
if ([appDelegate.connected containsObject:cellno]) { //condition
[cell.contentView addSubview:tickView];
}else{
[cell.contentView addSubview:imgView];
}
break;
}
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
During the first time the tableview is loaded the 'imgView' subview is added to the cells content view and the after some operation the 'if' condition is satisfied and the 'tickView' is added.
The problem is, the oldview is not hidden or removed and hence both the images appear.
Help would be greatly appreciated
Instead of creating your imgView and tickView views in the cellForRowAtIndexPath method, create them when you create the cell and reuse them with the cell. Then you can do this:
...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
}
...
if ([appDelegate.connected containsObject:cellno]) { //condition
[imgView removeFromSuperview];
[cell.contentView addSubview:tickView];
}else{
[tickView removeFromSuperview];
[cell.contentView addSubview:imgView];
}

I need sample code for creating a custom image-based accessoryType in TableView

Here is the code I have for the tableview customizations.
How would I make the accessory indicator be an image (arrow.png)
-(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];
// Code to wrap the text within the cell for larger text
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:# "Helvetica" size:17.0];
UIImageView * separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:# "separator.png"]];
separator.frame = CGRectMake((cell.frame.size.width - separator.frame.size.width) / 2,
CELL_HEIGHT - separator.frame.size.height,
separator.frame.size.width,
separator.frame.size.height);
[cell addSubview : separator];
[separator release];
}
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [titleList objectAtIndex:indexPath.row];
// display the disclosure button
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
All you need is
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
Be careful though as in editing mode the cell property you need to update is called editingAccessoryView