以往的方法是自定義uitableviewcell,中添加按鈕,然後將新建的mytableviewcell添加進入數組中,每次刷新列表在從數組中通過indexch.row獲取到數組的第幾個..這樣子就沒有使用到列表的重用機制了. 雖然速度上沒發現卡頓現象,但是感覺上還是有寫不足
而且現在都比較懶了,能不自定義uitalbeviewcell,就不要自定義了一個了.但是很糾結的一個問題就是想這樣寫
//組裝每一條的數據
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CustomCellIdentifier =@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell ==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier];
UIButton *btn = (UIButton *)[cell.accessoryView viewWithTag:83];
if (btn == nil) {
UIButton *btnNormal = [UIButton buttonWithType:UIButtonTypeCustom];
btnNormal.tag = 83;
[btnNormal setFrame:CGRectMake(0, 0, 60, 60)];
[btnNormal setImage:[UIImage imageNamed:@"imagebtn_off.png"] forState:UIControlStateNormal];
[btnNormal setImage:[UIImage imageNamed:@"imagebtn_on.png"] forState:UIControlStateSelected];
[btnNormal addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnNormal setTitle:@"添加" forState:UIControlStateNormal];
btnNormal.titleLabel.font=[UIFont boldSystemFontOfSize:20];
[btnNormal addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = btnNormal;
}
}
return cell;
}- (void)touchBtn: (id)sender {
UIButton *button =
(UIButton *)sender;
button.selected =
!button.selected;
這樣寫的話,卻是可以在cell上添加一個按鈕,而且,點擊按鈕之後按鈕狀態回定格在選擇的圖片,再次點擊回變回正常狀態
但是上面說了.這個是 static NSString *CustomCellIdentifier =@"CellIdentifier";
UITableViewCell *cell
= [tableView dequeueReusableCellWithIdentifier:
CustomCellIdentifier];重用的機制
所以在拉動列表之後,下面相對的幾個就會出現,有些cell中的按鈕跟着改變的狀態.
今天剛看到的新方法是可以在 - (void)touchBtn:
(id)sender這裏面去修改.
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview]; //通過這個獲取到點擊的是第幾個cell
int row =
[myTableView indexPathForCell:cell].row;
//myTableView是該列表
.....
獲取到列表的第幾個.就可以了....
以前沒有想到可以這個.今天,突然發現還可以這樣了.呵呵.小開心一下.
希望有所幫助.
我的微博:http://weibo.com/u/2299163507?source=blog 互粉交流哈.