【问题标题】:How to show buttons in uitableview cell and their action will be shown on label on the same cell如何在 uitableview 单元格中显示按钮,它们的操作将显示在同一单元格的标签上
【发布时间】:2013-01-24 19:37:29
【问题描述】:

在 UITableViewCell 中有不同的按钮,当我点击任何按钮时,它的动作正在所有单元格上执行,但我希望当我按下按钮时,动作将显示在同一个单元格的标签上,我知道我需要将它们放入数组中,但是如何...?

当我单击按钮时,一个值正在递增,它应该显示在同一单元格的标签上 这是代码:-

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
        likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
       [likeBtn addTarget:self action:@selector(likeFn) forControlEvents:UIControlEventTouchUpInside];
        likeBtn.frame=CGRectMake(0, 110, 90, 50);
        [likeBtn setTitle:@"Like" forState:UIControlStateNormal];
        [likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cell addSubview:likeBtn];
        [cell addSubview:likeShow];
        return cell;
}

这是按钮的动作

-(void)likeFn{
            NSString *str;
            NSMutableString *mystring=[NSMutableString string];
            likeCount++;
            str =[NSString stringWithFormat:@"%d",likeCount];
            NSString *world = @"Like";
            NSString *helloWorld = [world stringByAppendingString:str];
            likeShow.text=helloWorld;        
}

【问题讨论】:

  • 您确定对所有标签执行操作还是对最后创建的标签执行操作?
  • @sanjitshaw 是的,对标签执行的操作是最后创建的
  • 我只是想知道小东西你有多少节?
  • 不是我要求的单元格数。

标签: iphone ios objective-c uitableview


【解决方案1】:

试试下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



NSString *CellIdentifier = [NSString stringWithFormat:@"Cell-%d",indexPath.row];

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //[cell clearsContextBeforeDrawing];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

int lbltag = 1000;

UIButton *likeBtn = nil;
UILabel *likeShow = nil;

if ([cell viewWithTag:lbltag])
{
    likeShow = (UILabel*)[cell viewWithTag:lbltag];
}
else
{
    likeShow=[[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)] autorelease];
    likeShow.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:indexPath.row] intValue]];
    likeShow.tag = lbltag;
    [cell addSubview:likeShow];
}

if ([cell viewWithTag:indexPath.row+1])
{
    likeBtn = (UIButton*)[cell viewWithTag:indexPath.row+1];
}
else
{
    likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    likeBtn.tag = indexPath.row+1;
    [likeBtn addTarget:self action:@selector(likeFn:) forControlEvents:UIControlEventTouchUpInside];
    likeBtn.frame=CGRectMake(90, 0, 90, 50);
    [likeBtn setTitle:@"Like" forState:UIControlStateNormal];
    [likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cell addSubview:likeBtn];

}

return cell;
}

-(void)likeFn:(UIButton*)btnClicked
{
NSString *strLikes = [_marrTest objectAtIndex:btnClicked.tag-1];
int likeCount = [strLikes intValue] + 1;
[_marrTest replaceObjectAtIndex:btnClicked.tag-1 withObject:[NSString stringWithFormat:@"%d",likeCount]];

NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:btnClicked.tag-1 inSection:0];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];

UILabel *requiredLabel = (UILabel*)[cell viewWithTag:1000];

NSString *str = requiredLabel.text;

//str = [str stringByAppendingFormat:@"selected %@", str];
requiredLabel.text = @"";
requiredLabel.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:btnClicked.tag-1] intValue]];

//do what ever you want with the label
}

【讨论】:

  • 这段代码工作正常,当我向下滚动表格时,状态会丢失,第二个单元格显示类似计数标签
  • 非常感谢,这还有一个问题,我怎样才能为每个单元格设置单独的值,每个单元格都有自己的相似值,现在所有人都只有一个值.. 有你了解我的问题
  • 我认为你有一个类似值的数组,所以使用该数组来显示类似的值。我只为示例编写了该代码。
  • 你能修改这个代码为like数组吗,我做不到
  • 我想确保您有一个类似值的数组,以及该数组的内容(如果有的话)如何提供我作为示例。
【解决方案2】:

在单独的 nib 文件中创建单元格。为该单元格添加按钮和标签。 还要为该单元格创建一个视图类,并将 nib 文件中的单元格设置为您创建的类。将出口形式笔尖制作成类文件。还为按钮创建动作并实施动作。并在表格视图中使用该单元格。

【讨论】:

    【解决方案3】:
    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
         static NSString *CellIdentifier = @"Cell";
    
         UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
    
    
       likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
       [likeBtn addTarget:self action:@selector(likeFn::) forControlEvents:UIControlEventTouchUpInside];
        likeBtn.frame=CGRectMake(0, 110, 90, 50);
        [likeBtn setTitle:@"Like" forState:UIControlStateNormal];
        [likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cell.contentView addSubview:likeBtn];
        [cell.contentView addSubview:likeShow];
        return cell;
    }
    
     -(void)likeFn:(UIButton *)sender :(UIEvent *)event{
             NSSet *touches = [event allTouches];
             UITouch *touch = [touches anyObject];
              CGPoint currentTouchPosition = [touch locationInView:self.view];
              NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            NSString *str;
            NSMutableString *mystring=[NSMutableString string];
            likeCount++;
            str =[NSString stringWithFormat:@"%d",likeCount];
            NSString *world = @"Like";
            NSString *helloWorld = [world stringByAppendingString:str];
            for(id subView in cell.contentView.subviews){
            {
                   if([subView isKindOfClass:[UILabel class]]){
                         UILabel *tmpLabel = (UILabel *)subView;
                          tmpLabel.text=helloWorld; 
                   }
    
            }
    
        }
    

    试试这个代码,它可能对你有帮助

    【讨论】:

    • Terl- 当我点击第一个单元格上的点赞按钮时,它仍然在最后一个单元格上显示点赞标签
    • 您是否照原样复制了[likeBtn addTarget:self action:@selector(likeFn::) forControlEvents:UIControlEventTouchUpInside]; 这一行?
    • 是的,sanjit 的回答解决了我的问题,当我滚动表格时,标签状态丢失
    【解决方案4】:

    尝试将标签设置为:

    likeBtn.tag = indexPath.row;
    likeShow.tag = indexPath.row;
    

    likeFn:

    - (void)likeFn: (id)sender {
         UIButton * btn = (UIButton*)sender;
         //check if the btn tag and label tag are same.
    
    
    }
    

    【讨论】:

      【解决方案5】:

      检查此代码。这可能对您有帮助。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
      
      NSString *CellIdentifier = @"Cell";
      
      UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
      
      if (cell == nil)
      {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          [cell clearsContextBeforeDrawing];
      }
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
      likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
      
      
         likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
         [likeBtn addTarget:self action:@selector(likeFn) forControlEvents:UIControlEventTouchUpInside];
          likeBtn.frame=CGRectMake(0, 110, 90, 50);
          [likeBtn setTitle:@"Like" forState:UIControlStateNormal];
          [likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
          [cell addSubview:likeBtn];
          [cell addSubview:likeShow];
       return cell;
        }
      

      使用 didSelectRowAtIndexPath 方法

       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          return;
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多