當前位置:成語大全網 - 新華字典 - 滾動事件中如何區分tableview和scrollview?

滾動事件中如何區分tableview和scrollview?

滾動視圖:

1.介紹壹下scrollView的壹些屬性。

1 & gt;使用scrollView必須做兩件事。

1).設置scrollView內容。

2).設置內容大小(滾動範圍)

2 & gt。其他屬性

1).內容偏移量(滾動位置)

2).內容插入(額外滾動區)

3).彈跳(設置UIScrollView是否需要彈簧效果)

4).crollEnabled(設置UIScrollView是否可以滾動)

5).Showshorizontalscrollindicator(是否顯示水平滾動條)

6).顯示垂直滾動條(是否顯示垂直滾動條)

2.代理

1 & gt;代理思想的兩點思考

1).聽力思維:B聽聽a怎麽了。

2).通知思路:A有事,要通知B去做。

2 & gtscrollView的代理使用。

1).如何成為代理人(三步)

*聲明協議*設置代理對象self . scroll view . delegate = self;*實施協議的方法

2).代理監聽scrollView的拖動事件。

//開始拖動——(void)scroll view will begin拖動:(UIS scroll view *)scroll view;//結束拖動-(void)scrollviewdidendrawing:(uiscrollview *)scroll view將丟棄:(bool)丟棄;//滾動時執行-(void)scrollviewdidscroll:(uiscrollview *)scroll view。

3).使用代理實現縮放。

*成為UIScrollView()的代理*設置zoom對象(通過viewForZoomingInScrollView方法)*設置縮放到範圍(maximumZoomScale,minimumZoomScale)。

3.創建定時器有兩種方法。

1 & gt;. self . timer =[nstimer scheduledtimerwithtimeinterval:1 . f target:self selector:@ selector(method)userInfo:nil repeats:YES];當另壹個scrollView正在運行時,計時器的scrollView將被停止,並且只能執行壹個scrollView。

2 & gt. self . timer =[nstimer timerwithtimeinterval:1 . f target:self selector:@ selector(method)userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop]add timer:self . timer forMode:NSRunLoopCommonModes];

4.定制協議並使用它

1 & gt;定義協議(三個步驟)

*定義協議(兩個可選的[代理對象可以不實現]和必需的[代理對象必須實現])

*添加代理屬性(弱)@ property(弱,非原子)id < LFAppInfoViewDelegate & gt委派;

*向代理發送消息,調用代理方法(需要判斷代理對象是否實現方法,不需要判斷調用後是否報錯(編譯時不會報錯)。註意:定義協議的命名【類名+委托】和協議方法的命名規範【方法名需要帶前綴,以自身為參數】。

2 & gt使用代理(三個步驟)

*聲明協議

*設置代理對象

*實現協議的方法(在這種情況下,壹個UILabel被添加到代理對象[控制器])。

表格視圖:

1.UITableView需要設置壹個數據源來顯示數據。

1 & gt;。從數據源中會查詢到多少組,多少行,每組顯示什麽數據。

2 & gt數據源必須符合UITableViewDateSource協議。

3 & gt壹* * *有多少組?

-(nsinteger)number of section sintable view:(uitableview *)table view { }節組中有多少行?

-(nsinteger)table view:(uitableview *)table view number of row insertion:(nsinteger)section { }每行顯示什麽?

-(uitableviewcell *)table view:(uitableview *)table view cell for rowatindexpath:(nsindexpath *)indexpath {}節組的開頭顯示什麽標題-(NSString *)table view:(uitableview *)table view titleforheaderinsection:(nsinteger)節{ }節組的底部顯示什麽標題?

-(ns string *)table view:(uitableview *)table view tilefooterinsection:(nsinteger)section { }當每行單元格的高度不壹致時,使用proxy方法設置單元格的高度。

-(CG float)tableview:(uitableview *)rowatindexpath的table view高度:(nsindexpath *) indexxpath {}當每行的單元格高度壹致時,使用屬性設置單元格高度。

self . table view . row height = 60;可以優化內存的變量數組定義

NSMutableArray * models =[NSMutableArray array with capacity:(nsu integer)]

2.單元格的公共屬性

1 & gt;。單元格.文本標簽.文本標題

2 & gt簡介。cell.detailTextLabel.text

3 & gt。cell.imageView.image圖片

4 & gt。cell.accessoryView輔助視圖

5 & gt。cell.accessoryView自定義輔助視圖

6 & gt。cell.backgroundView設置單元格背景色。

1).單元格的背景可以由backgroundColor和backgroundView設置。

2).但是backgroundView的優先級高於backgroundColor。

3).所以如果同時設置了backgroundColor和background view,那麽background view就會覆蓋backgroundColor。

7 & gt。cell.selectedBackgroundView設置選定狀態的背景。

3.UITableView的通用屬性

1 & gt;。tableview.separatorStyle設置分割線樣式。

2 & gt。tableview.separatorColor設置分割線的顏色。自定義顏色。

[ui color color withdred:顏色值/255.f綠色:顏色值/255.f藍色:顏色值/255.f alpha:顏色值/255 . f];

獲取屏幕寬度:[ui screen main screen]. bounds . size . width;

3 & gt。tableview.tableHeaderView設置tableview的頁眉視圖壹般用於廣告。

4 & gt。table view . tablefooterview table view的底部視圖壹般用於放置加載更多按鈕。

5 & gt。[self . table view reload data];刷新表格//刷新指定的行。

NSIndexPath * path =[NSIndexPath indexPathForRow:row in section:0];[self . table view reloadrowsatindexpath:@[path]with row animation:uitableviewroanimationright];

4.優化單元的方法

1 & gt;先去緩存池查找是否有符合條件的單元格uitableviewcell * cell =[表視圖出列reusablecellwithdidentifier:identifier];

2 & gt如果緩存池中沒有符合條件的單元,則創建壹個單元if(nil = = cell){ cell =[[uitableviewcellalloc]init with style:uitableviewcellstyleubtitle重用標識符:標識符];}

3 & gt創建壹個單元格並設置唯壹的標簽:標識符註意:定義變量NSString *identifier建議使用static來定義靜態局部變量,而不是宏。

4 & gt。設置單元格數據並返回單元格。

5.表視圖代理方法

1 & gt;。-(void)table view:(uitableview *)table view didselectrowattindexpath:(nsindexpath *)index XPath { }//選中壹行時調用。

2 & gt。-(void)table view:(uitableview *)table view diddeselectrowatindexpath:(nsindexpath *)index XPath { }//未選中某行時調用。

3 & gt的壹些屬性和代理方法。UIAlertView

1).uialertview * alert =[[uialertviewalloc]init with title:@ "修改數據"消息:nil delegate:selfcancelbuttontitle:@ "取消"其他按鈕:@ "確定",nil];//創建壹個彈出窗口

2).alert . alertviewstyle = UIAlertViewStyle...;//設置alert的樣式,使alert可以顯示uitextfield。

3).UITextField * textField =[alert textField atindex:0];//獲取警報中的文本字段

4).[警報顯示];//顯示彈出窗口

5).-(void)alertview:(uialert view *)alert view clickbuttonindex:(n integer)button index { }//單擊按鈕時會調用alert view。

6.有兩種方法可以自定義單元格

1 & gt;純代碼:每個單元格子控件的數量和位置都不壹樣。

2 & gt。通過與xib相同的固定接口加載xib: cell:

1).[[[ns bundle main bundle]loadnibnamed:@ " xib name " owner:nil options:nil]first object];

2).uinib * nib =[uinib nibwithniibname:@ " xib name " bundle:nil];ui view * view =[[nib instantiate withowner:nil options:nil]first object];3 & gt。延遲調用dispatch _ after(dispatch _ time(dispatch _ time _ now,(int 64 _ t) (3.0 * nsec _ per _ sec)),dispatch _ get _ main _ queue(),{ });4 & gt的。只有在通過代碼創建控件時,才會調用init方法;只有在通過xib或storyboard創建控件時,才會調用AwakeFromNib方法。

5 & gt協議規範協議名稱:控件名稱+委托協議方法名稱:去掉前綴的控件名稱+含義。在協議方法中發出自己的(觸發的)控件的目的是為了方便區分哪個控件觸發了方法。

6 & gt。創建子控件的代碼,並將它們添加到content view[self . content view add subview:child controls];

7 & gt。計算文本寬度和高度cgsize * maxsize = cgsizemake (300,max float);//設置文本範圍ns dictionary * dict = @ { nsfont attributename:font };//Font//如果未來計算的字符範圍超過指定範圍,則返回指定範圍//如果未來計算的字符範圍小於指定範圍,則返回的是真實範圍CG size size =[ns string * STR bounding grectwithsize:maxSize Options:nsstringdrawingselinefragmentorigin Attributes:dict context:nil]。尺寸;//計算文本的寬度和高度

8 & gt通過代碼定制單元格的方法

1).創建壹個從UITableViewCell繼承的新類。

2).重寫initWithStyle:reuseIdentifier:方法添加所有需要顯示的子控件(子控件的數據和框架不需要,但子控件要添加到contentView)設置子控件的屬性壹次(有些屬性只需要設置壹次,比如font \ fixed picture)。

3).提供兩種模型數據模型:存儲文本數據的框架模型\圖片數據:框架高度\存儲數據模型的單元格\所有子控件4)。單元格有框架模型(不要直接擁有數據模型)。

5).重寫框架模型屬性的setter方法:在該方法中設置子控件的顯示數據和框架。

6)幀模型數據已經以惰性加載模式被初始化(對應於每個單元的幀模型數據僅被加載壹次)。

7.通知機制

1 & gt;。通知中心(NSNotificationCenter)每個應用都有壹個NSNotificationCenter的實例,負責輔助不同對象之間的消息通信創建通知中心NSNotification Center * Center =[NSNotification Center默認中心];

2 & gt壹個完整的通知壹般包含三個屬性:-(ns string *)name;//通知-(id)對象的名稱;//通知發布者(要發布通知的人)-(ns dictionary *)userInfo;//壹些額外的信息(通知發布者傳遞給通知接收者的信息內容)

3 & gt初始化notification object+(instance type)notification with name:(ns string *)an ame object:(id)an object;+(instance type)notification with name:(ns string *)aName對象:(id)an object userInfo:(ns dictionary *)ause rinfo;-(instance type)init with name:(ns string *)name對象:(id)object userInfo:(ns dictionary *)userInfo;

4 & gtNSNotificationCenter提供相應的方法發布通知——(void)post Notification:(ns Notification *)通知;//發布通知,可以設置通知名稱、通知發布者、附加信息等。在通知對象中-(void)post notification Name:(ns string *)an ame object:(ID)an object;//發布壹個名為aName的通知,anObject是這個通知的發布者——(void)post notification Name:(ns string *)an ame object:(ID)an object userinfo:(ns dictionary *)a userinfo;//發布名為aName的通知,其中anObject是此通知的發布者,aUserInfo是附加信息。

5 & gt。註冊通知監聽器(Observer)-(void)Add Observer:(ID)Observer選擇器:(SEL)壹個選擇器名稱:(ns string *)an ame Object:(ID)an Object;Observer: Listener,即接收這個通知的人。aSelector:收到通知後,回調偵聽器的這個方法,並傳入通知對象作為參數:通知的名稱。如果它是nil,監聽器可以接收這個通知,壹個Object:通知發布者,不管通知的名字是什麽。如果壹個對象和壹個名稱為零,偵聽器將接收所有通知。

6 & gt。取消註冊通知偵聽器通知中心不會保留偵聽器對象,並且在通知中心中註冊的對象必須在對象釋放之前取消註冊。否則,當相應的通知再次出現時,通知中心仍然會向監聽器發送消息。因為對應的listener對象已經釋放,所以可能會導致應用崩潰——(void)remove observer:(id)observer;-(void)remove observer:(id)observer name:(ns string *)an ame object:(id)an object;壹般在監聽器被銷毀之前就取消註冊(比如給監聽器添加下面的代碼):-(void)Dealloc {/[super Dealloc];這句話[[NSNotification Center默認中心] Remove Observer: self]需要在非ARC中調用;}

7 & gt。代理人的通知和選擇

1).***相同點

通知和代理都可以用來完成對象之間的通信。

2).差異

代理:壹對壹的關系(1對象只能告訴其他1對象發生了什麽)

通知:多對多關系(1個對象可以告訴N個對象發生了什麽,1個對象可以知道N個對象發生了什麽)。

8.鍵盤通知uikeyboardwillshownotion//鍵盤即將顯示uikeyboarddidshownotion//鍵盤顯示完成。UIKeyboardWillHideNotification//鍵盤即將隱藏uikeyboarddidhidinovation//鍵盤完全隱藏uikeyboardwillchangeframing//鍵盤的位置和大小即將改變uikeyboarddidchangeframing//鍵盤的位置和大小隨著與鍵盤相關的附加信息(字典)而改變。字典中常見的鍵如下:uikeyboardframebeginuserinfokey//鍵盤開頭的幀//鍵盤最後的幀(動畫結束後)。Uikeyboardanimation持續時間userinfokey//鍵盤動畫的時間uikeyboardanimation持續時間userinfokey//鍵盤動畫的執行節奏(速度)9。其他1 & gt;子控件不顯示調試方法。

1).檢查是否調用添加的方法。

2).框架為空(未設置框架)

3).是隱藏是嗎?

4).alpha & lt=0.1

5).未添加到父控件。

6).檢查老公控件是否有以上幾點,但是在init方法中得到的所有幀都是0-(void)layout subviews {[superlayoutsubviews];//當控件的框架改變時會調用此方法//此方法壹般用於調整子控件的位置} 2 >;。//已添加到父視圖時調用-(void)didmovetosuperview { }//即將添加到父視圖時調用-(void)willmovetosuperview:(ui view *)new superview { }。

3 & gt將左右視圖self . textfield . left view =[[uiviewalloc]initwithframe:cgrectmake(0,0,10,0)]添加到UITextField//設置左視圖self . textfield . left view mode = uitextfieldviewmodes的顯示模式;self . textfield . right view =[[ui view alloc]initWithFrame:CGRectMake(0,0,10,0)];//設置右視圖self . textfield . right view mode = uitextfieldviewmodes的顯示模式;

4 & gt。//設置btn中的圖片不要填滿整個imageviewbtn。imageview . content mode = uiviewcontentmodecenter;//不剪超範圍圖片//BTN . imageview . clipstobounds = no;BTN . imageview . layer . maskstobounds = NO;