【发布时间】:2011-11-30 03:55:50
【问题描述】:
我有一个表格视图。我最初使用单元格的 textLabel 来处理这个问题,但这不起作用,所以我以编程方式向每个单元格添加了一个 UILabel ,并且我有两个按钮,一个添加按钮和一个减去按钮,按下时基本上向上或向下计数。 (上升或下降1)两个按钮的方法是:
- (IBAction)addLabelText:(id)sender{
cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +1];
}
- (IBAction)subtractLabelText:(id)sender
{
if ( [[cellLabel text] intValue] == 0){
cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +0];
}
else{
cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] -1];
}
}
当我按下按钮并添加多个单元格时,按钮会干扰彼此的标签。所以我将这行代码添加到上述每个方法中。 :
cellLabel = (UILabel*)[sender superview];
希望它能解决问题。但它实际上添加了我再次相信单元格 textLabel。这是我按下一个添加了新行的按钮之前和之后的照片。
之后:
任何有关修复按钮相互干扰的帮助 UILabel 的请帮助! :D 谢谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
newBtn = [[UIButton alloc]init];
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(280,10,25,55)];
[newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"-" forState:UIControlStateNormal];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(250,10,25,55)];
[subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"+" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
cellLabel = [[UILabel alloc]init];
[cellLabel setFrame:CGRectMake(85, 22, 27, 27)];
[cellLabel setText:@"1"];
[cell addSubview:cellLabel];**
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
// cell.textLabel.text = [cells objectAtIndex:indexPath.row];
**if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle5.png"]]) {
[cellLabel setFrame:CGRectMake (195, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle4.png"]]) {
[cellLabel setFrame:CGRectMake (170, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle3.png"]]) {
[cellLabel setFrame:CGRectMake (140, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle2.png"]]) {
[cellLabel setFrame:CGRectMake (110, 22, 30, 30)];
}**
return cell;
}
【问题讨论】:
-
如果您正在为 iOS5 构建,您可能需要考虑使用 UIStepper 而不是两个 UIButton。
标签: iphone ios uitableview uibutton uilabel