如何在uitableview单元格中显示按钮及其操作将显示在同一单元格的标签上

时间:2013-01-24 07:04:25

标签: iphone ios objective-c uitableview

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

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

-(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;        
}

5 个答案:

答案 0 :(得分:4)

尝试以下代码

- (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
}

答案 1 :(得分:3)

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

答案 2 :(得分:1)

-(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; 
               }

        }

    }

尝试此代码可能会对您有所帮助

答案 3 :(得分:0)

尝试将标签设置为:

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.


}

答案 4 :(得分:0)

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

- (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;
  }

USE didSelectRowAtIndexPath方法

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

}