bitshine
bitshine

Reputation: 139

How can I draw a line from one cell to another cell when using UITableViewCell

I need to draw a chart which one cell has a line link to another cell, How can I do that in UITableViewCell? like below:

enter image description here

Upvotes: 0

Views: 70

Answers (2)

Aviel Gross
Aviel Gross

Reputation: 9965

Something like this?

NSIndexPath *iPathCellFrom = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *iPathCellTo = [NSIndexPath indexPathForRow:0 inSection:0];
CGRect from = [self.myTableView rectForRowAtIndexPath:iPathCellFrom];
CGRect to = [self.myTableView rectForRowAtIndexPath:iPathCellTo];
CGPoint fromCenter = CGPointMake(from.origin.x + from.size.width/2, from.origin.y + from.size.height/2);
CGPoint toCenter = CGPointMake(to.origin.x + to.size.width/2, to.origin.y + to.size.height/2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor]CGColor]);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, fromCenter.x, fromCenter.y);
CGContextAddLineToPoint(context, toCenter.x, toCenter.y);
CGContextStrokePath(context);
CGContextRestoreGState(context);

Context code taken from here

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77621

I'd use a UICollectionView instead of a table view.

Each cell will then contain one number.

You can then use the frame of each cell (and the current offset of the scroll view) to find the centre of each relevant cell.

Then use a view placed over the UICollectionViewCell and draw onto that.

Upvotes: 1

Related Questions