【问题标题】:Strange things happening to my table view我的表格视图发生了奇怪的事情
【发布时间】: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


【解决方案1】:

将标签设置为您配置它的单元格: 在我的表格中,每个部分都有一些部分和一些单元格。因此,第一个 (0) 部分的单元格具有标签 0...9999。从第二个 - 10000...19999 等等。我的意思是单元格的标签 = 10000 * 部分的数量加上这个部分中这个单元格的原始数量。然后,要计算这个单元格到底是什么,我看看有多少 10000 标签,从它是什么部分计算,以及有多少 >10000 标签必须从它的原始值计算。所以,我的表格视图中有部分和原始数据。

cell.contentView.tag = indexPath.row+10000*indexPath.section; //or any you like
[cell.contentView addSubview:button]; //adding your button

然后,在按钮的回调中:

int row=0;
int section=0;
long tag = [sender superview].tag;
NSLog(@"tag: %ld", tag);
for (int i = 0; i<[data count]; i++) {
    if (tag>=10000) 
    {
        tag-=10000;
        section++;
    }
    else i = [data count];
}
row = tag;

NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:section];
indexPath = [indexPath indexPathByAddingIndex:row];
[tableView cellForRowAtIndexPath:indexPath];

现在,您可以完全访问您的单元格。使用计算出的rawsection 来访问单元格。

【讨论】:

  • 我修改了我的答案。试试这个。
  • 好的,我应该把它放在subtractLabelText 方法中吗?另外,我复制并粘贴了您的代码,但我收到一个错误,抱怨其中一个 ] 括号,我无法弄清楚! ://
  • 是的,此代码适用于您的 subtractLabelTextaddLabelText,抱歉,我已修复。替换“-1”
  • 好的,非常感谢您的帮助!我将您的 UILabel*label 线切换到我已经创建的 UILabel cellLabel 但是,当我按下按钮时,应用程序崩溃了。我读了控制台,它说:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 4]'再次感谢!:D
  • 您确定您放置了 MINUS ONE 吗?您有 5 个对象 [0 .. 4] 并要求编号为 6,但应该是 4,这是您在配置单元格时添加的最后一个(这是您的标签,对吗?)。
【解决方案2】:

[sender superview] 返回您的 UIButton 的超级视图,即 UITableViewCell 也具有 text 属性,这就是为什么即使您错误地转换代码仍然可以正常运行的原因。

向我们展示您是如何构建和添加 cellLabel 的,以便我们了解它有什么问题。

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多