我现在已经尝试了将近一天,我有一个表视图,我调用重载数据来重新加载表的条目。 CellForRowAtIndexPath仅在第一次被调用,但在此之后,其他2个函数调用行数和节数,但不调用cellforRow。我检查过行和部分是非零的,链接是好的......这是代码。代码有点长,只需遵循UItableview的reloadData和delegate函数即可。 thx提前
@interface HMPeopleController ()
@property(nonatomic, retain) UIPopoverController *popOverHMPeople;
@end
@implementation HMPeopleController
@synthesize peopleCell,hmpeopleController,storedArrayForContacts;
@synthesize btnPeople;
@synthesize addingContactToPeopleList;
@synthesize popOverHMPeople,popOverEmail;
@synthesize attendees, btnIsHidden;
@synthesize peopleTableView,attendeeToBeAdded, meetingSummary, atendeeList, sortedArray,btnAddEmail,attendeeCount;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[hmpeopleController release];
[popOverEmail release];
[self.peopleTableView release];
[addingContactToPeopleList release];
[attendees release];
[attendeeCount release];
[sortedArray release];
[self setAttendeeToBeAdded:nil];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate * appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
if (appDelegate.joinOnlyMode) {
btnAddEmail.hidden=YES;
}
content = [AttendeesHeaderGenerator wordsFromLetters];
indices = [[content valueForKey:@"headerTitle"] retain];
self.sortedArray = [[NSMutableArray alloc]init];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"reload-data" object:nil];
int totalOnline=0;
for (HMAttendee *ha in [HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers) {
if (ha.userStatus) {
totalOnline++;
}
}
attendeeCount = [[UILabel alloc]initWithFrame:CGRectMake(125, 5, 22, 21)];
attendeeCount.text=[[NSString alloc]initWithFormat:@"%i",totalOnline];
[ attendeeCount setTextColor:[UIColor whiteColor] ];
[attendeeCount setBackgroundColor:[UIColor clearColor]];
[self.navigationController.view addSubview:attendeeCount];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doneFromHmpeople:) name:@"doneFromHmpeople" object:nil];
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if(!app.joinOnlyMode)
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Contacts" style:UIBarButtonItemStyleDone target:self action:@selector(addContactBtnPressed:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Email" style:UIBarButtonItemStyleDone target:self action:@selector(addEmailHandler:)];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
}
UILabel *lb=[[UILabel alloc]initWithFrame:CGRectMake(150,5,55, 21)];
[lb setText:@"Online"];
[lb setBackgroundColor:[UIColor clearColor]];
[lb setTextColor:[UIColor whiteColor]];
[self.navigationController.view addSubview:lb];
[lb release];
UIImageView *onlineIcon=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"button-onn.png"] ];
onlineIcon.frame=CGRectMake(98, 0, 32, 32);
[self.navigationController.view addSubview:onlineIcon];
[self.view setBackgroundColor:[UIColor clearColor]];
// self.title=@"Addtendees";
}
-(IBAction)DonePressed:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideatendeelist" object:nil];
}
-(void) reloadData:(id) sender
{
if (self.view.tag == 555) {
int users = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count];
numberOfRecords = users == 0 ? 1:users;
}
else
{
int users = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count];
if (users == numberOfRecords || (users-numberOfRecords) < 0) {
[btnPeople setTitle:[NSString stringWithFormat:@"%@",NSLocalizedString(@"People", nil)] forState:UIControlStateNormal];
}
else
{
//relalocate resources.
[btnPeople setTitle:[NSString stringWithFormat:@"%@(%d)",NSLocalizedString(@"People",nil),users-numberOfRecords] forState:UIControlStateNormal];
}
}
int totalOnline=0;
for (HMAttendee *ha in [HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers) {
if (ha.userStatus) {
totalOnline++;
}
}
attendeeCount.text=[[NSString alloc]initWithFormat:@"%i",totalOnline];
[sortedArray removeAllObjects];
NSLog(@"%i",[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb.count);
int total=[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb.count;
for (int i=0; i<total; i++) {
// HMAttendee *at = [[HMAttendee alloc] init];
HMAttendee *at = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb objectAtIndex:i ];
[sortedArray addObject:at];
}
[self.peopleTableView reloadData];
}
- (void)viewDidUnload
{
attendeeCount = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contactsSelected:)
name:@"contactsSelected"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hideAttendePopOver)
name:@"hideatendeePopOver"
object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if ([[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb count] > 0) {
NSArray *attendeeSecArray = [NSArray arrayWithArray:[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb];
self.sortedArray = [NSMutableArray arrayWithArray:[attendeeSecArray sectionedArrayUsingProperty:@"strName"]];
return [sortedArray count];
}else {
return 0;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
// int count = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count];
// int rowsInSec = 0;
// for (int i=0; i<count; i++) {
// HMAttendee* attdee = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers objectAtIndex:i];
// if ([[[content objectAtIndex:i] objectForKey:@"headerTitle"] isEqualToString:[attdee.strName substringToIndex:1]]){
// rowsInSec++;
// NSLog(@"Number of rows in section are %d", rowsInSec);
//
// }
// }
NSArray *sectionArray = [sortedArray objectAtIndex:section];
return [sectionArray count];
// return count == 0?1:count;
//return rowsInSec;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int count = [[sortedArray objectAtIndex:indexPath.section] count];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(@"[Presenter]", nil);
}
if (count == 0) {
cell.textLabel.text = [HMMainManager getSharedInstance].currentUser.strName;
}
else
{
HMAttendee* attdee = [[sortedArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = attdee.strName;
if(attdee.userStatus)
{
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
img.frame=CGRectMake(280, (cell.frame.size.height/2)/2, 16,16);
[cell addSubview:img];
[img release];
// cell.imageView.image=[UIImage imageNamed:@"1.png"];
// cell.imageView.frame=CGRectMake(280, cell.imageView.frame.origin.y, cell.imageView.image.size.width, cell.imageView.image.size.height);
}
else
{
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.png"]];
img.frame=CGRectMake(280,(cell.frame.size.height/2)/2, 16,16);
[cell addSubview:img];
[img release];
// cell.imageView.frame=CGRectMake(280, cell.imageView.frame.origin.y, cell.imageView.image.size.width, cell.imageView.image.size.height);
}
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
-(IBAction)addContactBtnPressed:(id)sender{
if ([self.popOverHMPeople isPopoverVisible]) {
[self.popOverHMPeople dismissPopoverAnimated:YES];
}
else
{
AddPeopleViewController *addPeopleController = nil;
addPeopleController =[[AddPeopleViewController alloc] initWithNibName:@"AddPeopleViewController" bundle:nil];
addPeopleController.delegate=self;
myContactsViewController *addressbookController=[[myContactsViewController alloc]initWithNibName:@"myContactsViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:addressbookController];
[self setPopOverHMPeople:[[[UIPopoverController alloc] initWithContentViewController:nav]autorelease]];
self.popOverHMPeople.popoverContentSize = CGSizeMake( 300,500);
// if ([self.popOverHMPeople isPopoverVisible]) {
//
// [self.popOverHMPeople dismissPopoverAnimated:YES];
//
// }
// else
// {
CGRect bound;
//bound=showAddressBookBtn.bounds;
// bound.origin.x = 720;
// bound.origin.y = 25;
[self.popOverHMPeople presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
// }
}
}
-(void) refresh
{
[btnPeople setTitle:NSLocalizedString(@"People" , nil)forState:UIControlStateNormal];
int count = [[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count];
numberOfRecords = ( count== 0) ? 1:count;
//add new added attendee
for (HMAttendee* attendee in attendeeToBeAdded){
NSMutableArray *storeArray = [[HMMainManager getSharedInstance].currentMeeting arrMeetingUsers];
BOOL found=NO;
for (HMAttendee *storeAtted in storeArray) {
if ([attendee.strName isEqualToString:storeAtted.strName]) {
found=YES;
}
}
if(!found)
[[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers addObject:attendee];
}
[peopleTableView reloadData];
}
-(void) customDataInitialization
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"reload-data" object:nil];
numberOfRecords = 1;
}
- (void)ContactsSelected:(NSMutableArray *)contacts {
NSLog(@"%d@",[[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count]);
[[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers addObjectsFromArray:attendees];
NSLog(@"%d@",[[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsers count]);
[peopleTableView reloadData];
}
- (void)doneFromHmpeople:(NSNotification *)notification{
if(popOverEmail.popoverVisible )
[popOverEmail dismissPopoverAnimated:YES];
// [self refresh];
}
- (IBAction)addEmailHandler:(UIBarButtonItem *)sender{
if ([popOverEmail isPopoverVisible]) {
[popOverEmail dismissPopoverAnimated:YES];
}
else {
hmpeopleController = [[HMPeopleControllerAddEmail alloc]initWithNibName:@"HMPeopleControllerAddEmail" bundle:nil];
UINavigationController * navEmail=[[UINavigationController alloc]initWithRootViewController:hmpeopleController];
popOverEmail=[[UIPopoverController alloc]initWithContentViewController:navEmail];
[popOverEmail setPopoverContentSize:CGSizeMake(hmpeopleController.view.frame.size.width, hmpeopleController.view.frame.size.height)];
[popOverEmail presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
- (BOOL)emailChecker:(NSString *)testStr{
NSError *error=nil;
NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:@"[a-zA-Z_.0-9]+(@){1}[a-zA-Z0-9]+(\\.){1}[a-zA-Z]{2,4}" options:NSRegularExpressionDotMatchesLineSeparators error:&error];
// NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:@"[a-zA-Z]+" options:NSRegularExpressionDotMatchesLineSeparators error:&error];
int matche=[regex numberOfMatchesInString:testStr options:0 range:NSMakeRange(0,testStr.length)];
NSArray* matches = [regex matchesInString:testStr options:0 range:NSMakeRange(0, [testStr length])];
if (matche<=0) {
return NO;
}
for ( NSTextCheckingResult* match in matches )
{
NSString* matchText = [testStr substringWithRange:[match range]];
if(![matchText isEqualToString:testStr])
{
NSLog(@"%@ =In valid",matchText);
return NO;
}
else if([matchText isEqualToString:testStr]) {
NSLog(@"%@",matchText);
return YES;
}
}
return nil;
}
- (void)contactsSelected:(NSNotification *)notification
{
attendees =[[NSMutableArray alloc]init];
// [self dismissModalViewControllerAnimated:YES];
NSDictionary *dict = [notification userInfo];
storedArrayForContacts=[dict objectForKey:@"key"];
NSMutableString *attendeesCanntBeAdded=[[NSMutableString alloc]init];
int count=[[dict objectForKey:@"key"] count];
if (count==0) {
[self.popOverHMPeople dismissPopoverAnimated:YES];
return;
}
NSMutableString* xmlDataString = [[NSMutableString alloc] init];
[self setAttendeeToBeAdded:nil];
attendeeToBeAdded = [[NSMutableArray alloc]init];
for (int i=0; i<count; i++) {
HMAttendee *atn= [[HMAttendee alloc]init];
atn.strName= [[[dict objectForKey:@"key"] objectAtIndex:i] objectForKey:@"Email"];
if (![self emailChecker:atn.strName]) {
[attendeesCanntBeAdded appendFormat:@"%@ ",atn.strName];
continue;
}
if([atn.strName length] > 0)
{
if ([attendeeToBeAdded count] == 0) {
NSMutableString* attendeeEmails = [[NSMutableString alloc] init];
[attendeeEmails appendString:@"<EmailID>"];
[attendeeEmails appendString:atn.strName];
[attendeeEmails appendString:@"</EmailID>"];
// [attendeeEmails appendString:atn.strName];
NSString *data = [NSString stringWithFormat:ADD_ATTENDEES_LIST_FORMAT,attendeeEmails];
[xmlDataString appendString:data];
// [attendeeEmails appendFormat:@"%@</EmailID><EmailID>",atn.strName];
[attendeeEmails release];
}else{
NSMutableString* attendeeEmails = [[NSMutableString alloc] init];
[attendeeEmails appendString:@"<EmailID>"];
[attendeeEmails appendString:atn.strName];
[attendeeEmails appendString:@"</EmailID>"];
NSString *email = [NSString stringWithFormat:ADD_ATTENDEES_LIST_FORMAT,attendeeEmails];
[xmlDataString appendString:(NSMutableString*)email];
}
[attendeeToBeAdded addObject:atn];
}
[atn release];
}
//[attendeeEmails appendString:@"</EmailID>"];
if (attendeesCanntBeAdded.length>0) {
NSString *st=[[NSString alloc]initWithFormat:@"Following Emails are invalid:%@",attendeesCanntBeAdded];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Skribbee" message:st delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[popOverEmail dismissPopoverAnimated:YES];
[alert show];
[alert release];
[attendeesCanntBeAdded release];
[st release];
}
if ([HMMainManager getSharedInstance].currentMeeting.isAttendeePopOver == true && [attendeeToBeAdded count] > 0) {
// NSString* aList = [NSString stringWithFormat:ADD_ATTENDEES_LIST_FORMAT,attendeeEmails];
[[HMMainManager getSharedInstance].currentMeeting addAttendee:@"" list:xmlDataString andDelegate:self];
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self.popOverHMPeople dismissPopoverAnimated:YES];
if(request.tag == ADD_ATTENDEE_REQUEST) {
NSLog(@"%d",request.responseStatusCode);
NSLog(@"%@",request.responseStatusMessage);
NSLog(@"%@",[request responseString]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"disableButton" object:nil];
[(AppDelegate *)[[UIApplication sharedApplication]delegate] hideActivityView];
if(request.responseStatusCode == 200)
// [self hideAttendePopOver];
{
NSDictionary *dic=[request.responseString JSONValue ];
NSString *rejectedEmails=[dic objectForKey:@"Description"];
int startPtr,endPtr,offset;
NSString *subStr;
if(![rejectedEmails isEqualToString:@"OK"])
{
for (int i=0; i<rejectedEmails.length; i++) {
unichar c=[rejectedEmails characterAtIndex:i];
NSString *tmpStr=[[NSString alloc]initWithCharacters:&c length:1];
if([tmpStr isEqualToString:@"("])
{
startPtr=i;
startPtr++;
}
if([tmpStr isEqualToString:@")"])
{
endPtr=i;
offset=endPtr-startPtr;
subStr=[rejectedEmails substringWithRange:NSMakeRange(startPtr, offset)];
break;
}
}//end for
startPtr=0,endPtr=0,offset=0;
NSMutableArray *tempMarray=[[NSMutableArray alloc]init];
// NSData *data=[subStr dataUsingEncoding:NSWindowsCP1251StringEncoding];
// NSString *textRev=[[NSString alloc]initWithData:data encoding:NSWindowsCP1251StringEncoding];
//
// NSLog(@" Actual String.. %@",textRev);
// subStr=textRev;
for (int i=0;i<subStr.length; i++) {
unichar c=[subStr characterAtIndex:i];
NSString *tmpStr=[[NSString alloc]initWithCharacters:&c length:1];
if(i == (subStr.length-1) )
{
int offset=i-startPtr+1;
NSString *subStr2=[subStr substringWithRange:NSMakeRange(startPtr, offset)];
if([subStr2 isEqualToString:@" "])
break;
NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:@"" forKey:@"Name"];
[tempDict setObject:subStr2 forKey:@"Email"];
[tempMarray addObject:tempDict];
startPtr=i+1;
}
if([tmpStr isEqualToString:@","])
{
int offset=i-startPtr;
NSString *subStr2=[subStr substringWithRange:NSMakeRange(startPtr, offset)];
NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:@"" forKey:@"Name"];
[tempDict setObject:subStr2 forKey:@"Email"];
[tempMarray addObject:tempDict];
startPtr=i+1;
}
}
NSMutableArray *tempArr=[[NSMutableArray alloc]initWithArray:attendeeToBeAdded];
for (NSDictionary *d2 in tempMarray) {
NSString *drstr=[d2 objectForKey:@"Email"];
drstr=[drstr stringByReplacingOccurrencesOfString:@" " withString:@""];
for (HMAttendee *d3 in tempArr) {
NSString *d3str=[d3.strName stringByReplacingOccurrencesOfString:@" " withString:@""];
if([d3str isEqualToString:drstr])
{
[attendeeToBeAdded removeObject:d3];
}
}
}
}//end OK if
NSString *code=[dic objectForKey:@"Code"];
NSLog(@"%@",[request responseString]);
[self refresh];
if([code isEqualToString:@"200.1"])
{
// UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Skribbee" message:[dic objectForKey:@"Description"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
// [alert show];
// [alert release];
}
else if([code isEqualToString:@"200"])
{
// UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Skribbee" message:@"Attendee Added Successfully!." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
// [alert show];
// [alert release];
}
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Skribbee" message:@"Your request was not completed successfully, please try again later!." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideatendeelist" object:nil];
}
-(void)hideAttendePopOver{
[self.popOverHMPeople dismissPopoverAnimated:YES];
}
@end
答案 0 :(得分:1)
在numberOfSectionsInTableView
你正在返回0.这就是为什么它不能正常工作。
另外可能的问题是如果[sortedArray count]
为零,那么numberOfSection将返回0.因此不会调用cellForRowAtIndexPath。使用NSLog检查数组的计数。
NSlog(@"Array count %d", [sortedArray count]);
更改方法如:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if ([[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb count] > 0)
{
NSArray *attendeeSecArray = [NSArray arrayWithArray:[HMMainManager getSharedInstance].currentMeeting.arrMeetingUsersForTb];
self.sortedArray = [NSMutableArray arrayWithArray:[attendeeSecArray sectionedArrayUsingProperty:@"strName"]];
return [sortedArray count];
}
else
{
return 1;
}
return 1;
}