我正在创建一个 iPad 应用程序,在该应用程序中我创建了一个自定义表视图,该视图根据 API 中的项目数返回行。每行包含三个带有背景图像和 tagID 的按钮。标签 ID 的目的是检测点击了哪个按钮并在该点击时触发一个事件,但不幸的是这不是这样发生的。
我的代码如下: -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
if (tableView==tableviewDetailLibrary)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor=[UIColor clearColor];
}
else if (tableView==tableviewMainLibrary)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor=[UIColor clearColor];
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"nav_bar_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"nav_bar_highlighted.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
}
}
if (tableView==tableviewDetailLibrary) {
int i1=0;
for(i=0; i<5; i++)
{
NSLog(@"i3 value is : %d", i);
int yy = 190 *i;// For Cell Height
for (int j=0;j<3; j++)
{
NSLog(@"ArrayCategories is : %d", [arrayCategories count]);
if (arrayIndex<[arrayCategories count])
{
CGRect rectTitle =CGRectMake(50+135*j, yy, 100, 40);
UILabel *lblTitle =[[UILabel alloc] initWithFrame:rectTitle];
lblTitle.text=[bookName objectAtIndex:arrayIndex];
lblTitle.textColor=[UIColor blackColor];
[cell.contentView addSubview:lblTitle];
CGRect rect = CGRectMake(50+135*j,yy+40 , 100, 150);// Adjust Cell width here 110
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
button.tag=arrayIndex+1;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayCategories objectAtIndex:arrayIndex]]];
if (imageData)
{
UIImage *buttonImageNormal=[UIImage imageWithData:imageData];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeCenter];
[cell.contentView addSubview:button];
NSLog(@" Button tag with image is : %d",button.tag);
}
else
{
[button setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeCenter];
[cell.contentView addSubview:button];
NSLog(@" Button tag without image is : %d",button.tag);
}
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button release];
arrayIndex++;
}
}
i1++;
}
}
if (tableView==tableviewMainLibrary) {
cell.textLabel.text=[arrayLibrayMain objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor colorWithRed:0.0f/255 green:208.0f/255 blue:255.0f/255 alpha:1.0];
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"nav_bar_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"nav_bar_highlighted.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:22];
}
return cell;
}
请向我建议一种在单击特定按钮时如何获取标签 ID 的方法。上面的代码工作正常。这是我的模拟器的屏幕不足。每当我点击一个按钮(图像)时,都会选择我不想要的整行。我只想要被点击的那个按钮标签。![在此处输入图像描述][1]
我已经在使用 [-(IBAction)buttonPressed:(UIButton *)btn] 方法,我以前没有写过,但现在我认为它应该在那里
-(IBAction)buttonPressed:(UIButton *)btn {
int tagId = btn.tag;
NSLog(@"Book is: %d",tagId);
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"http://miprojects.com.php5-16.ord1-1.websitetestlink.com/projects/evendor/api/download/index/apicall/Bookdownload/bookid/%d/apikey/998905797b8646fd44134910d1f88c33", tagId];
NSURL *url = [[NSURL alloc] initWithString:str];
NSLog(@"URL IS :%@",url);
urlArray=[[NSMutableArray alloc] init];
[urlArray addObject:url];
NSLog(@"URL Array %@",urlArray);
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[urlArray objectAtIndex:0]];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSLog(@"request : %@",request);
[request setUserInfo:dictionary];
NSLog(@"dictionary : %@",dictionary);
[downloadTableViewObj setDownloadDirectory:fileDestination];
[downloadTableViewObj addDownloadRequest:request];
}