今天做個**,突然發現在選中某行時打勾,再次選中其它行時,上次選中的行的勾還在,不能自動消失。
於是試了以下3種方法:
1、 //
第一種方法:在選中時先遍歷整個可見單元格,設定所有行的預設樣式,再設定選中的這行樣式,此方法不能取消單元格的選中
- (void
)tableview:(
uitableview
*)tableview didselectrowatindexpath:(
nsindexpath
*)indexpath
uitableviewcell *cell=[self.tableview
cellforrowatindexpath:indexpath];
cell.textlabel.textcolor=[uicolor bluecolor];
[cell setaccessorytype:uitableviewcellaccessorycheckmark]; }
此時只設定了在可見範圍內選擇的是一行,還得設定滾動後的選中狀態,
-(void
)tableview:(
uitableview
*)tableview willdisplaycell:(
uitableviewcell
*)cell forrowatindexpath:(
nsindexpath
*)indexpath
else
}單元格是否相同需要用到比較方法
nsindexpath *index=[tableview indexpathforselectedrow];
nscomparisonresult result=[indexpath compare:index]; 2、
//第二種方法:先定位到最後一行,若選中最後一行直接退出,否則用遞迴改變上次選中的狀態,重新設定本次選中的狀態。
- (uitableviewcell
*)tableview:(
uitableview
*)tableview cellforrowatindexpath:(
nsindexpath
*)indexpath
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath*)indexpath
uitableviewcell*newcell = [tableview cellforrowatindexpath:indexpath];
if(newcell.
accessorytype
==uitableviewcellaccessorynone)
nsindexpath*oldindexpath =[nsindexpath
indexpathforrow:current
insection:0];
uitableviewcell*oldcell = [tableview cellforrowatindexpath:oldindexpath];
if(oldcell.
accessorytype
==uitableviewcellaccessorycheckmark)
current=indexpath.row;
}- (
void
)tableview:(
uitableview
*)tableview didselectrowatindexpath:(
nsindexpath
*)indexpath
- (uitableviewcellaccessorytype)tableview:(uitableview*)tableview accessorytypeforrowwithindexpath:(nsindexpath*)indexpath
else
}或者直接在
-(uitableviewcell
*)tableview:(
uitableview
*)tableview cellforrowatindexpath:(
nsindexpath
*)indexpath裡面設定
單元格的預設高度為44
nslog(@"%@",nsstringfromcgrect(cell.frame));
設定選中時的背景顏色可以用selectedbackgroundview設定
計算UITableViewCell高度
uitableview是先執行 cgfloat tableview uitableview tableview heightforrowatindexpath nsindexpath indexpath函式計算整個uitableview內容高度,然後才執行 uitableviewcell table...
UITableViewCell重用問題
在寫sina 微博介面的過程中使用到了cell,那麼就是在cell上新增一些控制項,但是由於每條微博的內容都是不同的,所以在顯示的過程中,出現了內容重疊的問題,其實就是uitableviewcell重用機制的問題。cpp view plain copy uitableviewcell tablevi...
UITableViewCell重用機制
uitableview是ios開發中使用頻率非常高的乙個控制項,它常被用來展示資訊列表,儘管資訊資料可能非常多,但uitableview消耗的資源卻並不會隨著展示資訊的增多而變大,這都要得益於uitableviewcell的重用機制,重用機制 顧名思義,就是反覆利用資源的機制。以下通過一些 來看下通...