1.對於TableView ,每個item的視圖基本都是一樣的。不同的只有數據。
IOS提供了一種緩存視圖跟數據的方法。在 -UITableViewCell *) tableView:cellForRowAtIndexPath:
//創建一個用於緩存的標示
static NSString *ID=@"CellTable";
//先從緩存中取得UITableViewCell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
//如果取不到,則代碼創建。
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}//創建系統提供的每個item右邊的圖標
cell.accessoryType=UITableViewCellAccessoryCheckmark;[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationLeft];
#import@interface CSZViewController : UIViewController @property (weak, nonatomic) IBOutlet UITableView *tableView; - (IBAction)trashClick:(id)sender; @end
#pragma mark - dataSource
#pragma mark 每列行數
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
#pragma mark 創建每行的View
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//創建一個用於緩存的標示
static NSString *ID=@"CellTable";
//先從緩存中取得UITableViewCell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
//如果取不到,則代碼創建。
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text=self.array[indexPath.row];
cell.detailTextLabel.text=@"description....";
if ([self.deleteArr containsObject:cell.textLabel.text]) {
//創建系統提供的每個item右邊的圖標
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else
{
cell.accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - UITableViewDelegate
#pragma mark 點擊每個item調用
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.deleteArr containsObject:self.array[indexPath.row]]) {
[self.deleteArr removeObject:self.array[indexPath.row]];
[self.indexPaths removeObject:indexPath];
}else
{
[self.deleteArr addObject:self.array[indexPath.row]];
[self.indexPaths addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark 返回每行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}