Decrease Tableview selectable area in ios - ios

1Hi in my project there is a requirement to show table row selection(blue color) only half of the row.Is there any way to do this.
The selected cell should be in blue color and remaining all are in white. Please help me to achive this also.I dont want to reload table because many images are there loading from server.If reload again again it will slow down performance.
[Please see the image after selection the cell should appear like this]
enter image description here

You can set selectedBackgroundView property of UITableViewCell with a view whose half is blue.

In didSelectRowAtIndexpath get cell ..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// add view as per your according to need in cell
UIView * myCellAccessoryView = [UIview alloc] init];
// set frame of myCellAccessoryView as per your need ..
myCellAccessoryView.backGroundColor = // set color as you need .
// and add in cell
[cell.contentView addSubView :myCellAccessoryView];
}
hope it helps you ..

You can use this
UIView *backView = [[UIView alloc]init];
backView.frame = CGRectMake(0, 0, cell.frame.size.width/2, cell.frame.size.height);
backView.backgroundColor =[UIColor blueColor];
[cell addSubview:backView];
This will create a blue color background view.

Related

Set specific Frame for Background Color by Cell click

i have the following Custom Cell. On the left is a blue label with an image on it, and on the right side a label with some text. No i want that the cell gets highlighted when i click on it, BUT only the white part behind the label should get highlighted, not the whole cell with the blue label and the image, any ideas?
I tried something like this:
categorieCell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, categorieCell.frame.size.width, categorieCell.frame.size.width)];
categorieCell.selectedBackgroundView.backgroundColor = [UIColor redColor];
If you don't want the cell to be highlighted as a result of the selection, just set the cell's selectionStyle property to UITableViewCellSelectionStyleNone. That will disable overall cell's highlight when it gets selected.
In tableView cellForRowAtIndexPath method apply the following code:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
After that you can change cell background upon the cell gets selected.
In tableView didSelectRowAtIndexPath method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *categorieCell = [tableView cellForRowAtIndexPath:indexPath];
categorieCell.backgroundColor = [UIColor redColor];
}
Don't forget to change cell background when it gets deselected in willDeselectRowAtIndexPath:

How to make UITableView looks like this

is it possible to make a distance between cells like that in standard UITableView? There is no options to make separator bigger. Also this distance from right and left.
you can do this by set your cell background to whatever background you want (let's say gray in this case) and in the cell, add a white uiview with left, right, and bottom margin like this:
Then go to your table view, set the separator as none
and one last step, set the background of your table view to the same gray background as your cell.
Then you don't have to do anything particularly in your code besides simply initial your table cell based on the custom nib file (i assume you want to do this anyway).
If you prefer to construct your cell in codes, you can use the same logic.
in method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = [UIColor whiteColor];
// This will create a line of 3 pixels that will be separating the cells
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,3)];
separator.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separator];
// and if you want the border do left and right add:
UIView *separatorRx = [[UIView alloc] initWithFrame:CGRectMake(318,0,2,cell.frame.size.height)];
separatorRx.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separatorRx];
UIView *separatorSx = [[UIView alloc] initWithFrame:CGRectMake(0,0,2,cell.frame.size.height)];
separatorSx.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separatorSx];
return cell;
}
One way would be to set the backgroundView of the UITableViewCell to an UIImageView containing this white box on that gray background
You have to make a subclass of UITableViewCell. In your own subclass you may make everything you dream about! So, you need to add UIView with whiteColor background and margins as subview on your custom UITableViewCell.
There is not padding option in UITableView. You can either add a UIView in your UITableViewCell which will contains all the cell subviews, and change it's frame to create a padding right in the cell.
I suggest you to use UICollectionView instead, you can easily set the space between cells using a layout. If the cell is smaller than the actual collection View, then it's automatically centered.

Dynamically display an image in a UITableViewCell

I have a UITableViewCell, it is scroll-disabled and with fixed sections and rows.
There are two sections, with 1 row in section 0, and several rows in section 1.
The tableView is for users to make some choices.
So, the first section (with only one row) is going to display the result of users' choices,
and no doubt the second section (with several rows) is for choosing.
Now I want to put an image in the cell of the only row of the first section,
and this image will change according to users' choose.
It is very easy to judge which png image should be displaying, but I have trouble update it.
I tried use the cell's imageView, and manually alloc a UIImageView or UIView there to display those images.
But all of them won't work, I mean they just keep what they are displaying at the beginning and never changes it, even if I set the view's background or its image to a new png.
I tried some method like
[myImage setNeedsDisplay] for the manually alloced view,
or
[thatCell setNeedsDiaplsy] & [self.tableView reloadData] for the imageView of that cell,
but in vain.
So I wonder how can I achieve this function that dynamically display an image in a UITableViewCell in different situations?
Thanks a lot!
_____update line_____
I'm sorry that I didn't provide my code.
and here they are.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *inOutTableCellIdentifier = #"DealTableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inOutTableCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:inOutTableCellIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:cMyFont size:[UIFont systemFontSize]];
cell.detailTextLabel.font = [UIFont fontWithName:cMyFont size:[UIFont smallSystemFontSize]];
// I tried using both imageView and UIView here, but won't work
if (indexPath.section == 0) {
//cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"moneyCell.png"]];
cell.backgroundColor = [UIColor cyanColor];
// cell.imageView.image = [UIImage imageNamed:#"dealDone2.png"];
self.undoneIcon = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)] autorelease];
//self.undoneIcon.image = [UIImage imageNamed:#"dealUndone2.png"];
self.undoneIcon.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dealUndone2.png"]];
[cell.contentView addSubview:self.undoneIcon];
.... // deal with other rows in section 1
return cell;
}
// and this is the method that update the image, the images are named "dealDoneX.png",
// where X is an integer from 0 to 4.
- (void)checkUndoneDegree { // here I also tried 2 ways corresponding to UIView or a cell's imageView, but neither works well.
int i = 0;
if (self._date)
i++;
if (self.moneyTextField.text)
i++;
if (self._incomingAccount)
i++;
if (self._expensingAccount)
i++;
if (_dealType != kTransferView)
if (self._type)
i++;
NSLog(#"undone degree: %d",i);
NSString *imageName = [#"dealUndone" stringByAppendingFormat:#"%d",i];
self.undoneIcon.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
[self.undoneIcon setNeedsDisplay];
// NSUInteger p[2] = {0,0};
// UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndexes:p length:2]];
// [cell setNeedsDisplay];
}
and everytime I update the table's data, like changing some text of some cell,
I would call [self checkUndoneDegree] and then call [self.tableView reloadData],
But the picture is never updated, at least from the screen.
I even tried to put the codes that decide which png to set in the
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
method, but it can only make the view displaying the first png, without any updating.
Thanks for helping!
Make your undoneDegree (represented by i variable in your code) an ivar of your view controller, so it is accessible in all of it's methods, also in the UITableView delegate and data source methods.
Forget about setNeedsDisplay method. Since you are using UITableView to display your content, you need to play by its rules. This means you should use reloadSections:withRowAnimation: method.
Check again if you need self.undoneIcon. I'm pretty sure that imageView property should do. That is if you really want to display an image in the cell. If you want to create some kind of progress bar by manipulating cell's background, then use backgroundView property, or just place UIProgressView in your cell. This is what I think your want to do, after looking at your code.

Remove separator in grouped UITableView

I need to make a grouped table view with custom cells. Each cell must have a background image, so that image of one cell will touch the image of the second cell and so on. I've tried to set the separatorStyle to None, but I still get the transparent separator between cells.
Help me, please, remove this space between cells.
Have a good day,
Thanks!
I found that when I set the background image, the separator went away automatically. The problem is that I want it to show up.
Here's what I did:
Create a custom UITableViewCell class. In the respective init method do:
// Create a background image view.
self.backgroundView = [[UIImageView alloc] init];
Then in your controller that manages the UITableView:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// After all of the cell dequeue/allocation...
UIImageView *backgroundView = (UIImageView *)cell.backgroundView;
backgroundView.image = [UIImage imageNamed:#"your_image"];
}

Cell background incorrectly repeating in UITableViewCell when scrolling

I have a full list that populates a UITableView. This I want to background the one of the cells with a different color and it works initially, but for some reason when I start scrolling the table up and down, it starts drawing more cells with the green background.
Please note that there is always one detailCell.detailTimeLabel.text that's equal to currentTime.
The code I have is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... SOME MORE CODE HERE ...
if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
detailCell.backgroundView = backgroundView;
for (UIView* view in detailCell.contentView.subviews)
{
view.backgroundColor = [UIColor clearColor];
}
}
}
Which can be the problem?
Your trying to store data in a tableviewcell. You can't do this because the cells are constantly reused. The background you see repeated occurs because its the same cell being displayed over and over again with different text.
When you dequeue the cell, you need to reset it to blank and wipe out all the previous data. Then you should set the background color only if the data you are putting into the cell has the current time.
As a general rule, you should never refer to data in the cells. If you need to know what is in a particular cell, look at the data at the indexpath within the datamodel itself.

Resources