如有问题,联系邮箱:[email protected]
先给他简单的加个响应事件吧!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *titileString = [arrayobjectAtIndex:[indexPathrow]]; //这个表示选中的那个cell上的数据
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:titileStringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
效果如下图
其实在触发在cell访问的时候里面有个默认的style的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryNone, // don't show any accessory view //这个就是那个默认的吧
// UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
// UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
// UITableViewCellAccessoryCheckmark
}
我们接下来看看其他的3种效果
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
大多的时候表示点击cell 可以pus 到下个viewcontrol
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
这个是在cell 的右侧添加个button 来展示或触发其他的展示消失或推送! 但是这个是系统的button 大多数的应用都是自己定义的button我下说下系统的吧
这个时候我们需要另个一个delegate 和触发cell 的函数是不同的-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSString *titileString = [NSString stringWithFormat:@"你点击了按钮%@",[array objectAtIndex:[indexPath row]]];
UIAlertView *alert = [[ UIAlertView alloc]initWithTitle:@"提示" message:titileString delegate:self cancelButtonTitle:@"OK"otherButtonTitles: nil];
[alert show];
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
这个是选择数据时候 告诉我们选择了那行,感觉有点想我们加图片那节给cell加背景图的感觉!
先声明个全局变量
@property (strong,nonatomic)NSIndexPath *lastpath ;
让后在下面函数里面添加相应代码
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSUInteger row = [indexPathrow];
NSUInteger oldRow = [lastpathrow];
//如何点击当前的cell 最右边就会出现一个对号 ,在点击其他的cell 对号显示当前,上一个小时
cell.accessoryType = (row==oldRow &&lastpath !=nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = (lastpath != nil) ? [lastpath row] : -1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
lastpath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastpath = indexPath;
}
// 取消选择状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
效果如下图
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
mybutton.frame = CGRectMake(0, 0, 100, 25);
[mybutton setTitle:@"myButton" forState:UIControlStateNormal];
[mybutton setBackgroundImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
[mybutton addTarget:self action:@selector(myBtnClick:event:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = mybutton;
}
-(void)myBtnClick:(id)sender event:(id)event
{
NSSet *touches = [eventallTouches]; //把触摸的事件放到集合里
UITouch *touch = [touchesanyObject]; //把事件放到触摸的对象了
CGPoint currentTouchPosition = [touchlocationInView:self.tableview]; //把触发的这个点转成二位坐标
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:currentTouchPosition];//匹配坐标点
if(indexPath !=nil)
{
[selftableView:self.tableviewaccessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSString *titileString = [NSStringstringWithFormat:@"你点击了按钮%@",[arrayobjectAtIndex:[indexPath row]]];
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:titileStringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
效果如下图:
如果我不想指定的cell 有触发事件呢
我们会用到这个函数
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//我让第一个cell 点击的时候没有反应
if (indexPath.row ==0) {
returnnil;
}
return indexPath;
}
如有问题,联系邮箱:[email protected]