I want to show UITableViewCell selection style "Gray" without selecting the cell. How can I achieve this? But I want to show it only for the first row of the UITableView. Any suggestions?
if (indexPath.row == 0)
cell.backgroundColor = [UIColor greyColor];
Put it in :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if(indexPath.row == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.selected = YES;
}
...
}
Set the selectionStyle to Gray (this could be done in Interface Builder as well) and then set the cell as selected. Be sure to unselect the cell when it is reused.
You can do it in two ways:
First Way:
Why not put a view at the top as you want and place a tableview at the bottom of that view .
Second Way:
Customize a separate cell for your first row and use an if else condition when you are putting these cells in the table view .
Related
I have a section with dynamic multiple rows and I need to separate all the section with the background as shown in the image below.
Specifically those border lines for all the section. Pease let me know how can I make it possible.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return ProductArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr = [ProductArray objectAtIndex:section];
return arr.count;
}
Please refer to this
.
I tried adding header and footer, But I am not understanding how to add that rectangle box image for the entire section.
Put View in your cell after make outlet of that view. And use to cellForRowAtIndexPath this method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.viewBackGround.backgroundColor = [UIColor redColor];
return cell;
}
For above requirement, I would like to suggest you the below approach.
create only one section with multiple rows.
Each row of UITableView will have UITableViewCell
Inside UITableViewCell create content view for which we are gonna add the border
Each row of UITableViewCell will have another UITableView(inside content view) whose methods are handled by UITableViewCell cells
Change UITableView (inside) height based on number of rows.
provide height of UITableViewCell based on the number of rows + orderID Header.
I have a table view and I want to include margins so that the table's content has some breathing room on the left and right, and also between cells.
I managed to do a table view like:
My Storyboard design is like:
What I did was I added a UITableView to the main view and gave a margin of 10. I added constraints as shown in figure. Changed the Seperator style to None.
Then I added two UITableViewCells.
For Holding the data with custom row height 70.0
Row with same background of parentView with custom row height of 10.0
And implemented the methods like below:
// Row count (Twice as needed, for showing the insect)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 15*2;
}
// Returns cell based on indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Decides whether content or inset
if (indexPath.row%2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseInset"];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseMe"];
cell.textLabel.text = #"MMP";
}
return cell;
}
// Returns custom row height based on indexpath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row%2)
{
return 10.0;
}
return 70.0;
}
I'm having an issue that's appeared since upgrading to iOS7 in which when I try to change the background colour of a specific tableview cell, it doesn't colour the correct cells (usually the specified ones in addition to other ones). As you can see from my code below, I define the type that I want to be highlighted and then change the colour. It worked perfectly prior to the iOS upgrade so I'm not exactly sure what change has been made that's causing this:
Quick edit: also, when I scroll down the tableview and then back up, it colours more cells that weren't coloured when the tableview controller first loads (if that helps at all).
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
cell.backgroundColor = [UIColor colorWithRed:0.81 green:0.91 blue:0.81 alpha:1.0];
}
}
I think doing cell customization in tableView: cellForRowAtIndexPath: method is better. In this method,
if ([type isEqualToString:#"ace"])
{
cell.backgroundColor = [UIColor aceColor];
}
else // this else is important. If you add this, scrolling works fine.
{
cell.backgroundColor = [UIColor otherCellColor];
}
You likely have a single re-usable cell style. Consider having a re-usable cell style for your aces, and one for all others. Set the background color in cellForRowAtIndexPath, not willDisplayCell.
pseudo-code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
{
// load a cell with the background color desired
cell =
cell.backgroundColor =
.
.
.
return (cell);
}
// else a normal cell
cell =
.
.
.
}
I've a tableView with some cells. Each cell also contains a button. When the user clicks the button, the cell should be unclickable, but not the button. So when the user clicks on the button of a cell which is not clickable, this cell should be clickable again.
I tried:
cell.userInteractionEnabled = NO;
...but then the button wasn't clickable anymore.
Thanks to your effort in advance.
EDIT
I mean: When I click on a cell a new view opens. But I want, that no action happens, when the cell is not "clickable".
Unclickable in which way? If you just want the cell to not be selectable, you are probably seeking for this:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
If you want to prevent your code to be executed when the selection is disabled, just check for the selection property inside your didSelectRowAtIndexPath:method. Something like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
//(your code opening a new view)
}
}
Remember, you still have to play with this property, setting to UITableViewCellSelectionStyleNone when you don't want the cell to be selectable, and setting back to UITableViewCellSelectionStyleBlue (or UITableViewCellSelectionStyleGray) when you want it to be selectable again.
Swift version:
cell.selectionStyle = .none
Remove selection by setting UITableViewCellSelectionStyleNone as the selectionStyle.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
And do nothing in -tableView:didSelectRowAtIndexPath:
You can be selective in that delegate method for example if only the first row in the first section has the button and should do nothing :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *indexPathForDisabledCell = [NSIndexPath indexPathForRow:0
inSection:0];
if([indexPath compare:indexPathForDisabledCell] != NSOrderedSame) {
//Do whatever you do with other cells
}
}
This can also be done through Interface Builder using User Defined Runtime Attributes on the TableViewCell:
Key Path | Type | Value
selectionStyle | Number | 0
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/c/tdef/UITableViewCellStyle
In one part of my code, i call
UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
to get a number of cells, and then I change their alphas to .35. This works, and the cells look faded.
However, when I scroll the tableview, the cells that move out of the window and then move back are no longer are faded. This happens even though in cellForRowAtIndexPath the alpha for each disabled row is set to .35.
Right before returning the cells in cellForRowAtIndexPath I log
NSLog(#"%f", cell.alpha);
And it returns .35
However, the cell is not faded after scrolling? It appears as if it's alpha magically gets changed back to 1 before being displayed.
Following is my implementation of cellForRowAtIndexPath in my Section class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if ([self disabled])
{
cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:#"CellDisabled" style:UITableViewCellStyleDefault];
cell.alpha = .35;
}
else
{
cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:#"CellEnabled" style:UITableViewCellStyleDefault];
cell.alpha = 1;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//if-else block that only sets labels is here omitted
if (cell.textLabel.text == [[cs objectAtIndex:[self c]] type])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"%f", cell.alpha);
return cell;
}
And the view controller that coordinates these sections gets cells from them like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Alpha: %f", [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath].alpha);
return [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath];
}
(and the NSLog is logging appropriate alpha values)
Adding all subviews to cell's contentView and use the following string in -cellForRowAtIndexPath:
cell.contentView.alpha = {needed_value};
will works for you.
This issue is probably due to cells being reused in the table view. There is a better way to do this:
You should create an NSMutableDictionary in your view controller and just set keys for the indexes that should be faded.
Then, in cellForRowAtIndexPath, while you are setting up the cell, set the alpha based on whether or not the index exists as a key in the dictionary.
This solution will work without any weird behavior.
Make sure you are setting alpha value for cell.contentView too. Also set alpha in tableView:willDisplayCell:forRowAtIndexPath: as it might not work in iOS 7