我正在创建一个应用程序,其中有一个UITableView和表视图每个单元格都有一个UIView,而UIView包含两个自定义UIButton和两个UILabel标记)来自XML解析的标签的值。现在我必须做的是,当我点击任何一个按钮,然后它调用按钮的标签的方法,然后我想获取该单元格的两个标签的值,并使用该值。无法通过自定义方法从UITableViewCell获取值
但是,当我这样做这不是通过调用一个方法我得到的标签值,但是当我尝试获得标签值,然后我无法得到。
有没有什么办法来得到它的价值?
代码,我使用,以获取标签值的方法...
-(void)swapButtonPressed:(id)sender {
NSInteger a= [sender tag];
UITableViewCell *cell;
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) [[sender superview] superview]];
NSLog(@"%d", indexPath.row);
UIView *viewForCell = (UIView *)[cell.contentView viewWithTag:a];
UILabel *label = (UILabel *)[viewForCell viewWithTag:a];
NSString *str = label.text;
}
方法的cellForRowAtIndexPath的代码....
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSDictionary *dict = [appDelegate.webDataList objectAtIndex:indexPath.row];
tableView.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:0.39];
if([appDelegate.dataArray count]>0){
imgView.backgroundColor = [UIColor clearColor];
imgView =[[UIView alloc ] initWithFrame:CGRectMake(0,0,320,45)];
imgView.tag= indexPath.row;
[cell.contentView addSubview:imgView];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(291, 5, 19, 21);
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag= indexPath.row;
[imgView addSubview:button];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(-25, 5, 19, 21);
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag= indexPath.row;
[imgView addSubview:button];
button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(5, 7, 19, 21);
button.tag = indexPath.row;
img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"delete" ofType:@"png"]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:button];
NSArray *arr = [[appDelegate.dataArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"/"];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
label.text = [arr objectAtIndex:1];
label.tag = indexPath.row;
[label setFont:[UIFont fontWithName:@"Georgia" size:14]];
[label setNumberOfLines:0];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
label.backgroundColor = [UIColor clearColor];
[imgView addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)];
label.tag = indexPath.row;
label.text = [dict1 objectForKey:@"time"];
[label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
label.textAlignment = UITextAlignmentLeft;
[label setBackgroundColor:[UIColor clearColor]];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[label setTextColor:[UIColor whiteColor]];
[imgView addSubview:label];
[label release];
}
return cell;
}
你如何设置你的标签文本,也可以发布'cellForRowAtIndexPath`方法? – 2011-12-14 13:16:09
你可以给cellForIndex ...方法代码吗? – Maulik 2011-12-14 13:18:55