iOS tableview的每个cell中放了一个button实现点赞功能,如何解决复用问题

iOS tableview的每个cell中放了一个button实现点赞功能,如何解决复用问题,第1张

根据tag值进行获取,在这里设置tag值,

然后方法

-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath

中进行获取这个tag值,获取方法是:

UIButton exitBtn = (UIButton )[cell viewWithTag:1];//1是我上面设置的tag值

这样就获取到了,你再给他添加action事件就可以了,

cellarrowBtntag = indexPathrow +303;

[cellarrowBtn addTarget:self action:@selector(arrowBtnClick:) forControlEvents:UIControlEventTouchUpInside];

IOS6的UITableViewCell 子视图(subviews)的容器是UITableViewCellContentView

IOS7的UITableViewCell 子视图(subviews)的容器是UITableViewCellScrollView

由于IOS7中添加了滑动后出现编辑按钮的 *** 作,所以使用scrollView来处理,UITableViewCellScrollView有对触摸的相应处理,导致按钮的点击效果被屏蔽了,但是点击事件还是在的,所以可以通过在

1设置tableViewdelaysContentTouches = NO;

2同时在(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath代理方法中对scrollview把延迟触摸设置为NO即可

for (id obj in cellsubviews)

{

if ([NSStringFromClass([obj class])isEqualToString:@"UITableViewCellScrollView"])

{

UIScrollView scroll = (UIScrollView ) obj;

scrolldelaysContentTouches =NO;

break;

}

}

可以使用以下代码,但是有一点需要注意,如果你FlexGrid中数据行数过多不建议采用该方法,你可以设置该单元显示为一个,如何判断该单元格的单击事件:

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

private void Form2_Load(object sender, EventArgs e)

{

c1FlexGrid1DrawMode = C1WinC1FlexGridDrawModeEnumOwnerDraw;

c1FlexGrid1OwnerDrawCell += c1FlexGrid1_OwnerDrawCell;

c1FlexGrid1RowsCount = 20;

}

void c1FlexGrid1_OwnerDrawCell(object sender,C1WinC1FlexGridOwnerDrawCellEventArgs e)

{

if (eCol == 2 && eRow > 0)

{

Button button = new Button();

buttonName = stringFormat("FG_Button_{0}",eRow);

buttonText = "修改";

buttonClick += button_Click;

c1FlexGrid1ControlsAdd(button);

buttonLocation = new Point(eBoundsLeft, eBoundsTop);

buttonSize = new Size(eBoundsWidth, eBoundsHeight);

}

}

void button_Click(object sender, EventArgs e)

{

}

}

一种方式给Button加上tag值:这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。

[objc]view plaincopy

- (UITableViewCell)tableView:(UITableView)tableViewcellForRowAtIndexPath:(NSIndexPath)indexPath

{

staticNSStringidentifier =@"Cell";

UITableViewCellcell = [tableViewdequeueReusableCellWithIdentifier:reuseIdentifier];

if(cell ==nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

cellselectionStyle= UITableViewCellSelectionStyleNone;

}

Useruser = _users[indexPathrow];

celluser= user;

//拍照button

UIButton photographButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

photographButtonframe= CGRectMake(221,10,100,44);

[photographButtonsetImage:[UIImageimageNamed:@"camerapng"]forState:UIControlStateNormal];

[photographButtonaddTarget:selfaction:@selector(photographButtonClicked:)forControlEvents:UIControlEventTouchUpInside];

photographButtontag= indexPathrow;

[cellcontentViewaddSubview:photographButton];

returncell;

}

然后在点击事件中取数据,加信息

[objc]view plaincopy

- (void)photographButtonClicked:(UIButton)sender{

Useruser = _users[sendertag];

PhotoPickerControllerphotoPicker = [[PhotoPickerControlleralloc]init];

photoPickeruser= user;

[selfnavigationControllerpushViewController:photoPickeranimated:YES];

}

点击的时候确定你的button足够大,点击相应的是button,而不是cell,因为cell本身也是有点击回调的。其次你要在每次调

cell ForRowAtIndexPath方法里,

给cell的自定义属性赋值(比如你给cell定义了row属性)。然后在return cell。

如果你不重新赋值,那么因为cell的可重复利用性,某一行的cell再滚入屏幕时会重用上次回收该cell时的旧值。

以上就是关于iOS tableview的每个cell中放了一个button实现点赞功能,如何解决复用问题全部的内容,包括:iOS tableview的每个cell中放了一个button实现点赞功能,如何解决复用问题、新年好!TALBEVIEW的CELL中添加了一个BUTTON,怎么给它填上事件、ios7 tableviewcell上面有button,但是button的点击效果没有等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9649605.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存