Recent project needs, just to do a timeline, and iOS this aspect of the timeline is relatively few examples, I will do their own examples and ideas to share out to everyone, learn together.
The specific implementation effect of the timeline is shown in 1:
Figure 1
First step: To see this diagram, we think of the first reaction is to use TableView or CollectionView to complete, then I use the tableview here. First, create a single View application project, drag a TableView (shown in 2) into the Main.storyboard, and don't forget to point TableView delegate to Viewcontroller.
Figure 2
Step Two: TableView is finished, and then you can customize the Tableviewcell. In the project, select Cocoa Touch Class for new file, then select Inherit from Tableviewcell, then tick the xib file option, named "Timecoursetableviewcell", click Create, complete with 3:
Figure 3
Next, for this xib file layout, circled in Figure 4, we can find out what controls are in a cell.
Figure 4
According to this, the layout is shown in 5, mainly the label and view to complete the layout.
? ? ? ? ? ? ? Figure 5
In fact, there must be someone in question:
1. Why does the next one not use a button directly, but also to use a view inside nested a label it?
2. Why does the "content" label also add a parent control view?
In fact, at first, I did use a simple button and a label to think it would work. Later found that inexplicable appearance clearly set the Buttom and label frame, modified origin, but always appear in a messy position, later through the view nested inside, change the location of the view, it can be achieved completely. This bug I am also not clear, Hope insider informed, thank you.
Step three: Next, look at the code in the TimeCourseTableViewCell.h file inside the Xib:
#import <UIKit/UIKit.h> @interface timecoursetableviewcell:uitableviewcell@property (weak, nonatomic) Iboutlet UILabel *lbdate; Date @property (weak, nonatomic) Iboutlet UILabel *lbtime; Time @property (weak, nonatomic) Iboutlet UIView *infoview; The white bottom of the view@property (weak, nonatomic) Iboutlet UILabel *lbinfotitle; Notification title @property (weak, nonatomic) Iboutlet UILabel *lbinfocontent; Notification content @property (weak, nonatomic) Iboutlet UIView *infocontentview; Notification content of the parent view@property (weak, nonatomic) Iboutlet UILabel *lbsegment; Vertical gray dividing line @property (weak, nonatomic) Iboutlet UIView *receiveview; Crimson View@property (Weak, nonatomic) Iboutlet UILabel *lbreceive; Tap to receive notification-(cgfloat) Setcellheight: (NSString *) strinfo issameday: (BOOL) issame;-(void) setridues;-(void) setclick;-( void) Setnotclick; @end
The code in the timecoursetableviewcell.m file:
#import "TimeCourseTableViewCell.h" @implementation timecoursetableviewcell@synthesize lbdate @synthesize lbtime;@ Synthesize InfoView, @synthesize lbinfotitle, @synthesize lbinfocontent, @synthesize lbreceive; @synthesize Receiveview ; @synthesize lbsegment; @synthesize Infocontentview; -(void) awakefromnib {//initialization code}/* Remove the above date */-(void) deletedate{Lbdate.hidden = YES; CGRect timetemp = lbtime.frame; Timetemp.origin = Cgpointmake (timetemp.origin.x, 8); Lbtime.frame = timetemp; CGRect infoviewtemp = infoview.frame; Infoviewtemp.origin = Cgpointmake (infoviewtemp.origin.x, 8); Infoview.frame = infoviewtemp;} /* Sets the text height displayed to determine the height of the entire Tableviewcell, and finally returns the cell height */-(cgfloat) Setcellheight: (NSString *) strinfo issameday: (BOOL) Issame {/* If it is the same day, remove the date above */if (issame) {[Self deletedate]; } [Lbinfocontent setnumberoflines:0]; 0 lines, that is, according to the length of text, automatically increase the number of lines nsstring *labeltext = strinfo; nsmutableattributedstring *attributedstring = [[nsmutableattributedString alloc] Initwithstring:labeltext]; Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init]; [Paragraphstyle setlinespacing:5.0f];//adjust line spacing [attributedstring addattribute:nsparagraphstyleattributename value: Paragraphstyle range:nsmakerange (0, [labelText length])]; Lbinfocontent.attributedtext = attributedstring; [Lbinfocontent SizeToFit]; Fill//nslog (@ "%@", lbinfocontent); CGRect infocontentviewtemp = infocontentview.frame; Infocontentviewtemp.size = Cgsizemake (212, lbInfoContent.frame.size.height); Infocontentview.frame = infocontentviewtemp; /* Set Gesture click position */cgrect btntemp = receiveview.frame; Btntemp.origin = Cgpointmake (0, INFOCONTENTVIEW.FRAME.ORIGIN.Y + infoContentView.frame.size.height + 8); Receiveview.frame = btntemp; NSLog (@ "%@", Receiveview); /* Set the entire infoview height */cgrect viewtemp = infoview.frame; Viewtemp.size = Cgsizemake (viewTemp.size.width, RECEIVEVIEW.FRAME.ORIGIN.Y + ReceiveView.frame.size.heighT); Infoview.frame = viewtemp; NSLog (@ "%@", InfoView); Lbsegment.frame = CGRectMake (lbsegment.frame.origin.x, 0, 3, INFOVIEW.FRAME.ORIGIN.Y + infoView.frame.size.height + 8); NSLog (@ "HEight%f", INFOVIEW.FRAME.ORIGIN.Y + infoView.frame.size.height + 8); Return INFOVIEW.FRAME.ORIGIN.Y + InfoView.frame.size.height + 8;} /* Set round */-(void) setridues{LbTime.layer.cornerRadius = LBTIME.FRAME.SIZE.WIDTH/2; [Lbtime.layer Setmaskstobounds:yes];} /* Set Click to read style */-(void) setclick{cgfloat R = (cgfloat) 128/255.0; CGFloat G = (cgfloat) 128/255.0; CGFloat B = (cgfloat) 128/255.0; CGFloat alpha = (cgfloat) 1.0; Uicolor *colorrgb = [Uicolor colorwithred:r green:g Blue:b Alpha:alpha]; Receiveview.backgroundcolor = Colorrgb; Lbreceive.text = @ "I have received notification";} /* Set No click Reading style */-(void) setnotclick{cgfloat R = (cgfloat) 255/255.0; CGFloat G = (cgfloat) 83/255.0; CGFloat B = (cgfloat) 83/255.0; CGFloat alpha = (cgfloat) 1.0; Uicolor *colorrgb = [Uicolor colorwithred:r green:g Blue:b Alpha:alpha]; Receiveview.backgroundcolor = Colorrgb; Lbreceive.text = @ "Tap to receive notification";} -(void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated]; Configure The view for the selected state} @end
Next, look at the code in the VIEWCONTROLLER.M file:
#import "ViewController.h" #import "TimeCourseTableViewCell.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UITableView *messagetableview; @property int messagesum; Number of cells @property Nsarray *infoarray; Save information content @property Nsmutablearray *clickarray; The record has read information 1 has not received 0 receive @end @implementation viewcontroller@synthesize messagesum;-(void) viewdidload {[Super Viewdidl Oad]; Additional setup after loading the view, typically from a nib. /*tableview*/[self settableviewstyle]; Messagesum = 10; Self.infoarray = [[Nsarray alloc] initwithobjects:@ "The young man chats the youth crazy, cures the kidney deficiency, does not contain the sugar. ", @" kam hat mink, thousand rides with Kang Wang. "@" ", @" for the newspaper with the magistrate, 300 years, nine Chi Tang. Wine is still open chest, watermelon cream, happy lang. "@" holds the knots in the cloud, with three golden glucose. Will bow like a full moon, northwest of the Hope, Artie King. "@" Ten years of life and death two boundless, Hengyuanxiang, sheep and sheep. ", @" thousands of miles solitary tomb, laundry with the odd strong. ", @" even if meet should not know, complement dimension C, Schlkang. "@" The night came to the return of the dream, learning foreign languages, new Oriental. Were just no words, washing more healthy. Must be heartbroken every year, looking for work, Foxconn ", @ "La la la", Nil]; Self.clickarray = [[Nsmutablearray alloc] initwithobjects:@ "1" @ "1" @ "1", @ "1", @ "1", @ "1", @ "1", @ "1", @ "1", @ "1", nil];} #pragma mark-Set the background color of the Messagetableview and remove the split line-(void) settableviewstyle{Self.messageTableView.separatorStyle = Uitableviewcellseparatorstylenone; Remove Tabelview Split line cgfloat R = (cgfloat) 237/255.0; CGFloat G = (cgfloat) 237/255.0; CGFloat B = (cgfloat) 237/255.0; CGFloat alpha = (cgfloat) 1.0; Uicolor *colorrgb = [Uicolor colorwithred:r green:g Blue:b Alpha:alpha]; [Self.messagetableview Setbackgroundcolor:colorrgb];} #pragma mark-tabelview Data source protocol//This method specifies the number of rows in the table View-(Nsinteger) TableView: (UITAbleview *) TableView numberofrowsinsection: (nsinteger) section{return messagesum;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{//grouped by time, assuming here [0,2],[3,5],[6,9] is the same day Timecoursetableviewcell *nib = [[[NSBundle Mainbundle] loadnibnamed:@ "Timecoursetableviewcell" Owner:self Options:nil ] Lastobject]; if ([indexpath row] = = 0) {NSLog (@ "0"); return [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; } else if ([Indexpath row]<3) {NSLog (@ "0-3"); return [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:yes]; }else if ([indexpath row] = = 3) {NSLog (@ "3"); return [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; }else if ([Indexpath row] < 6) {NSLog (@ "3-6"); NSLog (@ "%f", [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:yes]); return [NIB SetceLlheight:[self.infoarray Objectatindex:[indexpath row] [Issameday:yes]; }else if ([indexpath row] = = 6) {NSLog (@ "6"); NSLog (@ "%f", [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]); return [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; }else if ([Indexpath row] > 6) {NSLog (@ "6-9"); NSLog (@ "%f", [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:yes]); return [nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:yes]; }else return 145;} The method returns the Cell object, how many rows the table is called, and how many times-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) indexpath{static NSString *simpleidentify = @ "Timecourse"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:simpleidentify]; Timecoursetableviewcell *nib = [[[NSBundle Mainbundle] loadnibnamed:@ "Timecoursetableviewcell" Owner:self OptiOns:nil] Lastobject]; [nib Settag:[indexpath row]; if ([[[Self.clickarray Objectatindex:[indexpath Row]] isequaltostring:@ "1"]) {[nib Setnotclick]; }else [nib Setclick];
[NIB Setridues]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapgesture:)]; nib.receiveView.userInteractionEnabled = YES; [Nib.receiveview Addgesturerecognizer:tap]; if ([indexpath row] = = 0) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; } else if ([Indexpath row]<3) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] Issameday:y ES]; }else if ([indexpath row] = = 3) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; }else if ([Indexpath row] < 6) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] Issameday:ye S]; }else if ([indexpath row] = = 6) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] issameday:no]; }else if ([Indexpath row] > 6) {[nib Setcellheight:[self.infoarray Objectatindex:[indexpath Row]] Issameday:ye S]; }
cell = nib; Cell.selectionstyle = Uitableviewcellselectionstylenone; Remove the provided selected No color return cell;} #pragma mark-tabelview Proxy protocol//Check event-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@ "");}-(void) Tapgesture: (UITapGestureRecognizer *) sender{NSLog (@ "receive notification"); UIView *view = Sender.self.view; Timecoursetableviewcell *cell = (Timecoursetableviewcell *) View.superview.superview.superview; UILabel *lb = view.subviews[0]; NSString *str = @ "Touch to receive notifications"; if ([Lb.text isequaltostring:str]) {[Cell Setclick]; [Self.clickarray setobject:@ "0" atIndexedSubscript:cell.tag]; }}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
So, after running, effect 6 shows:
? Figure 6
Change the color to complete the following:
?
So far, the entire timeline is complete.
The idea is probably as follows:
1. According to the time group: Judging is not the same day, if it is the above date hidden, the overall layout to move upward.
2. Get the cell clicked: by adding a tag to the cell, when I click the gesture, I can get out the cell and change the cell's style
May not write very clearly, we can take a serious look at the code.
Source Address free Download: http://download.csdn.net/detail/junlinfushi/8345531
Implementation of the iOS timeline