如何在iPhone的TableView中添加按钮

如何在iPhone的TableView中添加按钮

本文介绍了如何在iPhone的TableView中添加按钮控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在表格视图中显示记录.我的每一行都显示记录标题,一个个人资料图片,用户名.这一切都完美地展现了出来.但是现在我必须添加一个带有图像的按钮(facebook中的"like"图像).每当用户单击该特殊的喜欢"按钮时,就会发生一个事件.并且,只要用户单击行标题,就会加载新的nib文件.

I am showing records in tableview. My each row is displaying title of record, one profile image, username. It all is showing perfectly. But now I have to add one button with image ("like" image in facebook). Whenever user will click on that partcular "like" button an event should occur. And whenever user will click on title of row new nib file will be loaded.

1)我将如何添加应该定义事件发生方式的图像.2)如何区分点击事件的发生位置(标题或喜欢"图片).

1) How will I add image which should have an event occur method defined.2) How will I distinguish where clicked event occurred ( title OR "like" image).

这是代码的一部分,该代码在表中增加价值,并在每行中添加个人资料图片.

Here are part of code which is adding value in table and adding profile images in each row.

// Code which is adding image of facebook and Twitter in each row as per data


if([[imageData objectAtIndex:[indexPath row]]isEqualToString:@"facebook"])
{

    UIImage *leftImage=[UIImage imageNamed: @"facebook.png"];
    cell.imageView.image= leftImage;

}
else if([[imageData objectAtIndex:[indexPath row]]isEqualToString:@"twitter"]){
    UIImage *leftImage=[UIImage imageNamed: @"twitter_bird_normal.jpg"];
    cell.imageView.image= leftImage;
}

这是在表中添加值的代码

and here is code which is adding values in table

...
...
[tableList addObject:facebookValue]; //facebookValue is string parsing from JSON
...
...

推荐答案

创建一个类型为UIButtonTypeCustom的UIButton将其图像设置为您的leftImage.将目标和选择器添加到指向委托和方法的按钮上.这是您正在寻找的事件.另外,用indexPath.row标记按钮,以便知道它来自哪一行.将按钮作为子视图添加到单元格.

Create a UIButton with type: UIButtonTypeCustomSet it's image to your leftImage.Add a target and selector to the button that points to a delegate and a method. This is the event you're looking for.Also, tag the button with the indexPath.row so you know which row it came from.Add the button as a subview to the cell.

这篇关于如何在iPhone的TableView中添加按钮控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:26