我有一个包含1个部分和2个行的tableview, 我只有一个按钮动作。
在按钮操作中,我必须检查单击了哪个单元格,因为根据该单元格,我必须打开电子邮件视图以发送邮件。
我创建了两个bool值,但它无法正常工作,我的emailview仍然在没有单元格的情况下进行调用 这段代码我在做什么
@implementation View
@synthesize Mytableview,selectedIndexPaths,value1,value2,cellselected;
BOOL values[1];
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath row] == 0 && [indexPath section] == 0)
{
cell.textLabel.text= @"test";
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
value1=TRUE;
}
else if ([indexPath row] == 1 && [indexPath section] == 0)
{
cell.textLabel.text= @"test";
value2=TRUE;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[Mytableview deselectRowAtIndexPath:indexPath animated:YES];
//self.cellselected = indexPath.row;
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
values[indexPath.row] = !values[indexPath.row];
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
-(IBAction)ExportEmail
{
if (value1==FALSE) {
return;
}
else {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
}
}
}
答案 0 :(得分:0)
声明类可验证的NSIndexPath;
NSIndexPath *myIndexPath;
将didSelectRowAtIndexPath修改为
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
myIndexPath = indexPath;
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
您可以在Button的IBAction中使用NSIndexPath,根据索引,您可以打开电子邮件。
-(IBAction)ExportEmail
{
if(myIndexPath.row==0){
// send mail
}else if(myIndexPath.row==1){
// send mail
}
}