2013-03-27 28 views
15

Tôi có XIB tệp TimerCell.xib với UITableViewCell. Trong lớp khác trong cellForRowAtIndexPath tôi khởi tạo này UITableViewCell:Cách đặt Hành động cho UIButton trong UITableViewCell

static NSString *CellIdentifier = @"cellTimer"; 
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

Trong tôi TimerCell Tôi có hai UILabel và một UIButton. Đối với nút này, tôi muốn đặt hành động cho một số phương pháp.

Tôi có thể làm như thế nào? Và làm thế nào để hiển thị trong UILabel dữ liệu đầu tiên từ đồng hồ đếm ngược nền của tôi trong thời gian thực?

Trả lời

25

Đoạn mã này sẽ giúp bạn

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.tag=indexPath.row; 
    [button addTarget:self 
     action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"cellButton" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0); 
    [cell.contentView addSubview:button]; 
    } 

    return cell; 
} 


-(void)aMethod:(UIButton*)sender 
{ 
NSLog(@"I Clicked a button %d",sender.tag); 
} 

Hope this helps !!!

+0

nó hoạt động! Cảm ơn bạn!! Làm thế nào để đặt thẻ cho nó ngay bây giờ? Trước khi nó được ** cell.staticButton.tag = indexPath.row; ** – Romowski

+0

Kiểm tra câu trả lời đã chỉnh sửa của tôi @Romowski – Dany

-3

Hãy nghiên cứu tốt hơn trước khi đăng lên SO. Đây là đoạn mã để thêm nút để di

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[Button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
[Button setTitle:@"ButtonTitle" forState:UIControlStateNormal]; 
Button.frame = CGRectMake(100.0, 10.0, 40.0, 40.0); 
[cell.contentView addSubview:Button]; 
+0

Tại sao nó lại được bình chọn? Hãy giải thích hoặc nó là vô ích. Cái này sai rồi hả? Liệu nó trả lời hoặc cố gắng trả lời câu hỏi? – YSC

17

Vì bạn có một lớp con của UITableViewCell tên TimerCell, bạn có thể thêm các thuộc tính ổ cắm để TimerCell. Ví dụ:

@interface TimerCell : UITableViewCell 

@property (nonatomic, strong) UIButton *button; 
@property (nonatomic, strong) UILabel *label; 

@end 

Trong TimerCell.xib, kết nối các cửa hàng với nút và nhãn.

Sau đó, trong tableView:cellForRowAtIndexPath:, bạn có thể dễ dàng truy cập vào nút để thiết lập mục tiêu và hành động của mình:

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

[cell.button addTarget:self action:@selector(cellButtonWasTapped:) 
    forControlEvents:UIControlEventTouchUpInside];; 

Bạn có thể truy cập vào nhãn của tế bào sử dụng tài sản label của nó để cập nhật nó khi cháy hẹn giờ.

Một cách khác để lấy nút và nhãn là sử dụng thẻ xem, như tôi đã mô tả trong this answer.

Trong cellButtonWasTapped: (hoặc bất kỳ hành động nào bạn gửi từ nút), có thể bạn sẽ muốn tìm đường dẫn chỉ mục của ô chứa nút. Tôi giải thích cách thực hiện điều đó trong this answer.

+0

Tôi không hiểu làm thế nào tôi có thể hiển thị giá trị bộ đếm thời gian hiện tại trong UITableViewCell ... bạn có thể giải thích điều này một lần nữa. Để biết thêm chi tiết: hẹn giờ hoạt động trong singleton. Chỉ có 1 bộ đếm thời gian, nhưng có một từ điển với một số mục có chứa giá trị thời gian. Mỗi bộ đếm thời gian phút giảm 1 từ tất cả các mục trong từ điển. TimerCell là một ô của tableView xuất hiện trong cửa sổ bật lên. Vì vậy, tôi muốn hiển thị giá trị thời gian của mục từ điển trong cell.label của tôi ... Cảm ơn – Romowski

+0

http://stackoverflow.com/questions/15653729/how-to-show-nstimers-time-value-in-uitableviewcells-label – Romowski

+0

1 để có giải thích và tài liệu tuyệt vời ... 10 người khác để đi với bạn 119.000;) – logixologist

3
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"cellTimer"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    NSInteger index = indexPath.row; 
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:101]; 
    UIButton *button = (UIButton *)[cell viewWithTag:100]; 
    [button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
} 

Và thiết UIButton và UILabel của thẻ trong file XIB bạn TimerCell.xib

Các vấn đề liên quan