sai*_*ram 2 objective-c uibutton uitableview ios
我有一个数组要在tableview中显示.我需要在最后一个+ 1单元格中的一个按钮.表示有20个元素的数组我需要21个单元格中的一个按钮如何做到这一点.我需要给该按钮提供动作..怎么做告诉我代码...提前谢谢.
小智 6
您将需要管理比数组元素数多一个的单元格.因此,请确保在表视图数据源实现中进行以下修改:
// return the correct row count
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [theArray count] + 1;
}
// detect the last cell and add it a subview
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
...
// detect the last row
if(indexPath.row == [theArray count]) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
// set the frame and title you want
[b setFrame:CGRectMake(0, 0, 50, 50)];
[b setTitle:@"button" forState:UIControlStateNormal];
// set action/target you want
[b addTarget:self
action:@selector(theActionYouWant:)
forControlEvents:UIControlEventTouchDragInside];
[cell addSubview:b];
}
else {
// configure a classic cell
}
return cell;
}
// the action method
-(void)theActionYouWant:(id)sender {
// handle the action
}
Run Code Online (Sandbox Code Playgroud)
作为替代方案,为什么不使用整个单元来执行操作?使用相同的机制管理另一个单元格,将其设置为自定义标签,并在选择检测到单元格时,发送消息:
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == [theArray count]) {
// the extra cell is selected, send a message
}
else {
// others cells selection handler
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7179 次 |
| 最近记录: |