您的位置:首页 > 产品设计 > UI/UE

UITableView删除移动多选其他基本操作总结

2014-06-18 19:19 495 查看
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"删除");
if (UITableViewCellEditingStyleDelete == editingStyle) {
// 删除 cell
// 先对数据源(提供数据的数组)进行删除

[self.array removeObjectAtIndex:indexPath.row];

// 第一个参数:要删除的cell的indexPath组成的数组
// 第二个参数:删除时的动画
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

if (UITableViewCellEditingStyleInsert == editingStyle) {
// 编辑数据源
[self.array insertObject:@"aa" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

}


// tableView的移动
// 1.设置cell是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

// tableView移动完成调用方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 先取除要移动的 对象(根据sourceIndexPath)
NSString *tempName = [[self.array objectAtIndex:sourceIndexPath.row] copy];
[self.array removeObjectAtIndex:sourceIndexPath.row];
// 将取出的元素(tempName)添加到目的地 (destinationIndexPath)
[self.array insertObject:tempName atIndex:destinationIndexPath.row];

[tempName release];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
// 系统的编辑按钮的触发方法
[super setEditing:editing animated:animated];

// 通过系统的编辑状态的改变 来改变tableView的状态
[_tableView setEditing:editing animated:animated];
}


// 给每个区起一个标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (0 == section) {
        return @"456456";
    }
    return @"123123";
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    label.backgroundColor = [UIColor greenColor];
    label.text = @"aaaa";
    return [label autorelease];
}


// 定义右侧的索引信息
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建一个重用标识字符串
static NSString *str = @"cellReuse";
// [2]
// 利用tableView内部的重用池(NSSet) 产生cell
// 1.从重用池中取一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
<p style="margin-top: 0px; margin-bottom: 0px; font-family: 'Heiti SC Light';"><span style="font-family: Menlo;">    </span><span style="font-family: Menlo;">// </span>如果同一个<span style="font-family: Menlo;">tableView</span>里面需要使用多个不同布局的<span style="font-family: Menlo;">cell,</span>在这个方法中需要创建多个不同的<span style="font-family: Menlo;"> </span>重用标识<span style="font-family: Menlo;">, </span>用于区分不同的<span style="font-family: Menlo;">cell</span></p>
// 2.判断取出的cell是否是空(nil)
if (nil == cell) {
// 如果重用池中没有现成的cell,就自己创建一个cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
NSLog(@"创建cell");
}
// 3.给cell的label赋值 获取当前的位置(indexPath)对应的数组 元素
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"a.JPG"];
// 设定cell的辅助视图
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
// 4.将获得的cell返回
return cell;
}

新添加一个知识:

// 系统提供了一个方法 在创建tableView的时候,允许tableView注册一个cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"str"]; 然后删掉判断重用池中的判断cell == nil 这段代码

缺点:无法确定cell的样式,一般只是做单方面的cell的处理

// 设定tableView的样子

// 分割线的样式
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
// 分割线的颜色
[tableView setSeparatorColor:[UIColor blackColor]];
// cell的高度(每行的高度)
[tableView setRowHeight:100];


// 开启viewController的编辑状态
self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 插入|删除 既为多选
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 可以根据indexPath判断
return YES;
}

// viewController的点击 编辑按钮 的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
NSLog(@"%d", editing);
[super setEditing:editing animated:animated];

// 开启tableView的编辑状态
[self.tableView setEditing:editing animated:animated];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: