所选单元格的Xcode标签

时间:2014-11-06 16:48:04

标签: objective-c uitableview uibutton

我想在按下单元格中的按钮时在自定义单元格中启动计时器标签。如何在所选单元格中显示该标签?我使用tag将标签连接到我的表视图。 感谢

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

NSString *CellIdentifier;

CellIdentifier = @"Cell01";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

// Button
Button = (UIButton *)[cell viewWithTag:104];
[Button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

theLabel = (UILabel *)[cell viewWithTag:105];

return cell; }

这是按钮动作

-(void)ButtonClicked:(id)sender {

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.TableView indexPathForCell:clickedCell];
NSInteger ClickedRow = clickedButtonPath.row;
// In the clickedButtonPath, this allow us to find out the row that was selected by the user.

NSLog(@"%@",clickedCell);

switch(ClickedRow)
{
    case 1:
    {
        // first row
    }
        break;

    case 2:
    {
        // second row
    }
        break;
}}

1 个答案:

答案 0 :(得分:1)

第一个错误是您没有创建自定义单元格,您创建的单元格没有视图104和105.更多代码:

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

// this 1.-
NSString *CellIdentifier;

 CellIdentifier = @"Cell01";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

 // Configure the cell...

 // Here you create a new UITableViewCell.


if (cell == nil)
 {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
 // this 2.
 // Button
 Button = (UIButton *)[cell viewWithTag:104];

 // This is nil. You can add
 NSLog(@"See this button %@",[Button description]);
 // And see nil in the console.

 [Button addTarget:self action:@selector(ButtonClicked:)      forControlEvents:UIControlEventTouchUpInside];

 // The same thing this label
theLabel = (UILabel *)[cell viewWithTag:105];



return cell; }

如果您正在使用Storyboards(或界面构建器),请在stroryboard的单元格中确保reuseIdentifier为Cell01,并在以下内容之间更改代码:此1.-和此2.-由:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell01" forIndexPath:indexPath];   

最后一步将是: 1.-在Storyboard或代码中退出默认选择行为,在代码中你必须在上面添加它 细胞创造了。

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

2.-添加didSelect并撤消:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *theLabel = (UILabel *)[selectedCell viewWithTag:105];
theLabel.text = @"New Selected text";
 }

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *theLabel = (UILabel *)[selectedCell viewWithTag:105];
theLabel.text = @"Non Selected text";

}