In single screen I have added UITableview and UICollectionview. I have added default delegate and datasource methods for both TableView and Collection View. but UITableView only little slow to load data. That data I have added statically that too load very slowly. Same code working fast in iPhone but iPad I am facing this slowness issues. Anyone know about this please help me do this.
I have found solution for that, Actually in my tableviewcellforrowatindexpath function i made some changes .
My current code following as,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellId = #"SimpleTableId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell1 == nil) {
cell1 = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if(values == 1)
{
return cell;
}
else if(values == 2)
{
return cell1;
}
else
{
return cell;
}
}
I have changed like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellId = #"SimpleTableId";
if(values == 1)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
else if(values == 2)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:cellId];
}
return cell;
}
}
Now my slowness issues solved as much expected.
Related
This question already has answers here:
UITableView Multiple Selection
(11 answers)
Closed 6 years ago.
I know this question has been asked before, but there is a confusion among those. As i need multiple checkmark on cell and on clicking again on that cell checkmark should be set to none for that selected cell. i have tried this but all cell get selected doesnot none when again clicking on selected cell. i have tried this.
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myTableViewCell" forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
and in didSelectRowAtIndexPath
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myTableViewCell" forIndexPath:indexPath];
if ( cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
and in didDeselectRowAtIndexPath
-(void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath
{
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myTableViewCell" forIndexPath:indexPath];
if ( cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
Please help.
You just need to maintain the state of cell whether is it selected or not.
Try this in your cellForRowAtIndexPath.
if ([tableView.indexPathsForSelectedRows containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
UiTableViewCell is not displaying in Storybord,
UiTableViewCell is not displaying in Storybord,UITableViewCell is not displaying in Storybord can any one help me Thanks in advance
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactsCell";
ContactsCell *cell = (ContactsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
Have you set Delegate and DataSource of you UITableView?
Using,
[self.IByourTableView setDelegate:self];
[self.IByourTableView setDataSource:self];
Update #1: For answer comment(ie: Open ChatViewController),
for that use delegate method of UITableView
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var storyBoardName = "Main"
var storyBoard:UIStoryboard?
storyBoard = UIStoryboard(name: storyBoardName, bundle: nil)
var gotoViewController: UIViewController? = storyBoard?.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as? UIViewController
navigationController?.pushViewController(gotoViewController!, animated: true)
}
Note:Go to Story board and give Identifier to your ChatViewController
you wrote nothing to display in table
in cellforrow at indexpath add below line
cell.textlable.text=#"add something";
or replace your cellforrowatindexpath with below code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactsCell";
ContactsCell *cell = (ContactsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textlable.text=#"add something";
return cell;
}
Do you know that you can't make UITableViewCell appear in storyboard by writing code? All the code you wrote will only display in the device.
You want UITableViewCell to appear in storyboard then you need to open storyboard and start adding and editing your uitableviewcell not by writing code.
In my table view I've two sections with different custom cell for both section.I want to select one row from each section.Is it possible?
Here is my approach -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"categoryCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
if (indexPath == selectedCategoryIndexPath)
{
catCell.selected = YES;
}
else
{
catCell.selected = NO;
}
catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
}
else
{
static NSString *CellIdentifier = #"durationCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportDurationCell* durationCell = (ReportDurationCell *)cell;
if (indexPath == selectedDurationIndexPath) {
durationCell.selected = YES;
}
else
{
durationCell.selected = NO;
}
durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
}
cell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
selectedCategoryIndexPath = indexPath;
}
else
{
selectedDurationIndexPath = indexPath;
}
}
But it select only one row from either section(i want to select one row from each section).also i need first row of each section selected by default.
Add a bool property in your custom cell's header files. and make following changes (Assuming that you have initialised selectedCategoryIndexPath and selectedDurationIndexPath appropriately) -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"categoryCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
if (indexPath.row == selectedCategoryIndexPath.row) {
catCell.isSelected = YES;
}
else
catCell.isSelected = NO;
catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
catCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
catCell.selectionStyle = UITableViewCellSelectionStyleNone;
return catCell;
}
else
{
static NSString *CellIdentifier = #"durationCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ReportDurationCell* durationCell = (ReportDurationCell *)cell;
if (indexPath.row == selectedDurationIndexPath.row) {
durationCell.isSelected = YES;
}
else
durationCell.isSelected = NO;
durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
durationCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
durationCell.selectionStyle = UITableViewCellSelectionStyleNone;
return durationCell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
selectedCategoryIndexPath = indexPath;
}
else
{
selectedDurationIndexPath = indexPath;
}
[self.tableView reloadData];
}
When i select index 0 or 1 or any else that same time it also selected index according to above index
i need solution
there are many rows and when i select cell 1 same time another cell selected automatically..
#property (nonatomic, retain) NSIndexPath * checkedIndexPath;
in ViewDidLoad
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];//Default 1st row will be checkMarked
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSimpleCell];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSimpleCell]autorelease];
}
if (self.checkedIndexPath== indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *currentcell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
currentcell.accessoryType = UITableViewCellAccessoryNone;
self.checkedIndexPath = indexPath;
UITableViewCell *currentcell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
currentcell.accessoryType = UITableViewCellAccessoryCheckmark;
}
I want to apply checkmark as accessory type to a selected item in a uitable view. but the checkmark appears for all reused cells. How can we avoid this problem?
code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
// Configure the cell...
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}
Try this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell; }
Hope this will help you.
you have to add the else case to unmark the cells that you don't want to be marked, like this:
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
Try it this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}