Separator Color of one cell - objective-c

how can i change the color of the separator line of my table view? But i want change only the first line.
This works for the whole tableview.
tableView.separatorColor = [UIColor blueColor];

Subclass UITableViewCell and put a line UIView at the top of the cell that can be accessed via a property (in storyboard or in code, whatever you like)
In cellForRowAtIndexPath set the color of the line view according to the row.
If you only want to change the first line, and by that you mean the top one as opposed to the line in between the first and second rows, then consider using UITableView's tableHeaderView.

try this
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
tableView.separatorColor = UIColor.blueColor()
} else {
tableView.separatorColor = UIColor.greenColor()
}
}

Related

UICollectionView fails to honour zPosition and zIndex - iOS 11 only

I try to find a workaround for this radar: http://www.openradar.me/34308893
Currently in my - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes I'm doing the following:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC * 50), dispatch_get_main_queue(), ^{
self.layer.zPosition = 0;
});
But no matter how many NSEC_PER_MSEC I enter it doesn't work quite right, the scrolling indicator sometimes hides behind the headers, especially when ne headers appear. Is there another/better approach?
I had a similar problem where a header view in a UICollectionView was getting rendered above another view that had it's zIndex set > 0 in a custom UICollectionViewFlowLayout.
The answer for me was to set the header view layer's zPosition in willDisplaySupplementaryView:
override public func collectionView(_ collectionView: UICollectionView,
willDisplaySupplementaryView view: UICollectionReusableView,
forElementKind elementKind: String,
at indexPath: IndexPath) {
if elementKind == UICollectionView.elementKindSectionHeader && type(of: view) == CollectionViewSectionHeaderView.self {
view.layer.zPosition = -1
}
}
iOS11 doesn't seem to honor z-indexes in UICollectionViewFlowLayout subclasses.

Hide grey color when cell is touched

I'm making an app with a multiple cell selection (I use checkmark) like this
The problem is that when I click on a cell, it remains gray and is really bad. How can I fix it?
If you do not want to show grey selection at all:
Select the UITableViewCell in Storyboard
In Inspector, set selected to none
You can find more options to set it programatically in this answer.
try this code at didSelectRowAt#
cell.selectionStyle = UITableViewCellSelectionStyleNone
You can use this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
or
cell.selectionStyle = UITableViewCellSelectionStyle.None

UITableViewAutomaticDimension with imageview

I have a tableview that contains a custom cell. The custom cell has an imageview and a label. I want to resize the cell based on the text in the label because the imageview dimensions are constant. The imageview takes up the majority of the cell being centered and the label is below it.
self->tableView.rowHeight = UITableViewAutomaticDimension;
I am using this line of code to do so, and I have also set up constraints on the storyboard. When the code runs the image does not show, only the label and the cell is not even resized based off of the text. When I set the view to a default height, both are shown. I am unsure why this is not working. My only thought is that my constraints are incorrect. I have successfully used this with 2 labels but not an imageview. Any help would be appreciated. I would show a picture regarding my constraints, but I can not due to my status.
If you have layout like this of your tableViewCell then set your constraints as shown in below.
You also need to set estimatedRowHeight of your tableView.
self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Remove height constraint form label if set and set label noOflines = 0. It will help you.
Make you set the following
-label numberOfLines to 0
-don't set height for label
-in the bottom contraint between the label and the cell contentView, set it to greater than or equal to e.g 5
Then call the methods below
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Toggle view inside a cell row?

I have a UITableView with some contents. The cells have their own UITableViewCell class that has two UIView and a UILabel properties. When the user taps a cell, one of the views changes it's color. But if I want to return the previous color, it doesn't happen.
I tried with custom BOOL variable, with some methods inside the UITableViewCell class, everything - no success.
(Note: I tried with the UITableViewCellAccessoryType accessory (I don't want cell accessory, I just tried it) and it worked......)
So, how can I toggle a view/label/anything inside a cell? Thank you.
p.s. I can show you code, but it seems more theoretical question......
Usually you override setSelected: in your table view cell subclass and change to color there (depending on the selected state.
Like this:
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if (selected) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor whiteColor];
}
}

How to make specific UITableViewCell to be visible on screen while having more rows in UITableView

I am having 20 rows in a table view, and I have designed (or say resized) UITableView in half of the screen (Using IB) and in half screen I am showing the stuff related to particular UITableViewCell. Which cell's details are going to be shown in the half screen is decided runtime. I want the cell being visible while loading the view for say second last cell.
How can I achieve this?
I found the easiest way to ensure that a whole cell is visible is to ask the tableview to make sure that the rect for the cell is visible:
[tableView scrollRectToVisible:[tableView rectForRowAtIndexPath:indexPath] animated:YES];
An easy way to test it is just to put that into -(void)tableView:didSelectRowAtIndexPath: - tapping any cell will then scroll it into place if it's partially hidden.
UITableView class offers :
-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated;
which seems to be what you are looking for.
The NSIndexPath object can be built using :
+(NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
Try scrollToRowAtIndexPath:atScrollPosition:animated::
NSUInteger indexArray[] = {1,15};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:2];
[yourTableView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionTop animated:YES];
Swift 4 version of Nico teWinkel's answer, which worked best for me:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.scrollToRow(at: indexPath, at: .middle, animated: true) //scrolls cell to middle of screen
}
Swift 5 version of Nico teWinkel's answer making use of UITableview's scroll rect to visible method.
let indexPath = IndexPath(row: 0, section: 0)
let cellRect = myTableView.rectForRow(at: indexPath)
myTableView.scrollRectToVisible(cellRect, animated: true)
Use like this,
let indexPath = IndexPath(row:row, section:section)
self.tableView.scrollToRow(at:indexPath, at:.top, animated:true)