iOS 7 UITableView Not Responsive - ios

I am trying to implement a UITableView. My issue is that tableview cells, in order to be selected in simulator, require the user to press and hold that cell. I don't understand why this is happening. I would like cell's to be selected with a simple tap. Here is my code. There isn't much.
The header file:
#import "BasisViewController.h"
#define kDISCUSSIONS 5
#interface MyDiscussionsViewController : BasisViewController <UITableViewDataSource,UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *myDiscussionsTable;
#end
The implementation file:
#import "MyDiscussionsViewController.h"
#interface MyDiscussionsViewController ()
#end
#implementation MyDiscussionsViewController
#synthesize myDiscussionsTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Annoyingly, iOS 7 makes it so the seperators of table views are offset slightly. Get rid of this property.
if ([myDiscussionsTable respondsToSelector:#selector(setSeparatorInset:)]) {
[myDiscussionsTable setSeparatorInset:UIEdgeInsetsZero];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return kDISCUSSIONS;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TheDiscussions";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell.textLabel setText:#"This Discussion"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"User selected discussion at index: %d",indexPath.row);
}
#end

Related

tableView:cellForRowAtIndexPath: in iOS Today Widget with UITableViewController called once

I'm trying to display data in Today Widget extension in UIViewController subclass. The number of items (rows) is alway 3 but the tableView:cellForRowAtIndexPath: is called only once.
I've double-checked everything but cannot find a bug.
Any suggestions?
Thanks!
#import "TodayViewController.h"
#import "FLWAccount.h"
#import "FLWAccountTableViewCell.h"
#import "FLWAuthManager.h"
#import <NotificationCenter/NotificationCenter.h>
#interface TodayViewController () <NCWidgetProviding, UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, readwrite, weak) IBOutlet UITableView
*tableView;
#end
#implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 79.0f;
self.tableView.rowHeight = 79.0f;
[self updatePreferredContentSize];
[self.tableView reloadData];}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
[[FLWAuthManager sharedManager] updateAllAccountsWithCompletion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self updatePreferredContentSize];
[self.tableView reloadData];
completionHandler(NCUpdateResultNewData);
});
}]; }
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger count = [FLWAuthManager sharedManager].accounts.count;
return count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FLWAccountTableViewCell *cell = (FLWAccountTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FLWAccountTableViewCell" forIndexPath:indexPath];
cell.account = [FLWAuthManager sharedManager].accounts[indexPath.row];
return cell; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 79.0f; }
- (void)updatePreferredContentSize {
NSInteger count = [FLWAuthManager sharedManager].accounts.count;
self.preferredContentSize = CGSizeMake(self.preferredContentSize.width, 79.0f * count); }
#end
If you are using storyboard, check if the table view content is set to "Dynamic Prototypes" (and NOT "Static cells")

TitleLabel is broken when you put a UIButton in UITableViewCell

I have UITableView with custom UITableViewCell. Custom cell contains UIButton and UILabel.
Here I observe that UILabel text is change as I expected but UIButton text is not changing.
When I scroll out the button outside of the screen, change the label of UIButton.
Why it is not working? I have use Xcode 6 and use below code.
ViewController.h
#import <UIKit/UIKit.h>
#import "customTableViewCell.h"
#interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerNib:[UINib nibWithNibName:#"customTableViewCell" bundle:nil] forCellReuseIdentifier:#"customTableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
cell.button.titleLabel.text = #"Label does not change immediately";
cell.label.text = #"change label";
return cell;
}
customeTableViewCell.h
#import <UIKit/UIKit.h>
#interface customTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet UILabel *label;
#end
This is because the UIButton class has multiple states (Normal, Selected, Highlighted, Disabled). When you change the text property of it's internal UILabel (textLabel property of UIButton), it's property is overriden by setState function, which is called when table is loaded.
To change text in the label of a button you need to call setTitle:forState: method. Here is your code fixed to work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
[cell.button setTitle:#"Now it works" forState:UIControlStateNormal];
cell.label.text = #"change label";
return cell;
}
For the sake of completion, you can also use setAttributedTitle:forState: method with NSAttributedString, so you can actually set your own specifically formatted string as a title.
Use this
[cell.button setTitle:#"MyNewTitle" forState:UIControlStateNormal];

How to programmatically turn ViewController into a UITableViewController

I currently have a MatchCenterViewController that I want to programmatically turn into a UITableViewController. I've attempted to do so below based on tutorials I've found, but it doesn't seem to be appearing.
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
#interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
#end
#implementation MatchCenterViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"newFriendCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//etc.
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
#end
As a minimum, you need to implement the following methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
And you need to set the delegate and datasource, typically in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
Also, you need an IBOutlet to the table view if the table view was created in storyboard, or a property for the table view, if the table view was created in code.

How to make a Sub Table view in a TableViewCell

I have a TableView displaying a list of domain :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.dataSource = mainClass.domainList;
}
The domain list is set up like that :
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.domainList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = [[self.domainList objectAtIndex:indexPath.row] name];
return cell;
}
This works perfectly, it displays each domain in a row of my table view.
Now i would like to add a "Sub TableView" in each cell of my Table View to display a list of documents related to the domain.
I tried that :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *table = [[UITableView alloc] init];
table.dataSource = [self.domainList objectAtIndex:indexPath.row];
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = [[self.domainList objectAtIndex:indexPath.row] name];
[cell.contentView addSubView table]
return cell;
}
It doesn't crash but it doesn't work neither. I mean the sublist doesn't appear anywhere.
What am i doing wrong?
datasource must be a class implementing the protocol UITableViewDataSource. It looks like you are setting it to a custom object. Create a separate class with the code you used to implement the first table, then set the sublist as source data. In the objc.io article “clean table view code” they explain how to make reusable datasources. Or you can just give it a try on your own.
Consider this code:
// ARRAYDATASOURCE.H
#import <Foundation/Foundation.h>
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
#interface ArrayDataSource : NSObject <UITableViewDataSource>
-(id) init __attribute__((unavailable("disabled, try initWithItems:cellIdentifier:configureCellBlock")));
- (id) initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
#end
// ARRAYDATASOURCE.M
#import "ArrayDataSource.h"
#interface ArrayDataSource ()
#property (nonatomic, strong) NSArray *items;
#property (nonatomic, copy) NSString *cellIdentifier;
#property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
#end
#implementation ArrayDataSource
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
self = [super init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
self.configureCellBlock(cell, item);
return cell;
}
#end

Method "tableView:didSelectedRowAtIndexPath" in a UITableView embedded into a UIViewController

I've a UITableView embedded into a UIViewController, I've followed all the steps needed to display a list into the table view and this is working
I've a problem with the following method, it looks like it is invoked but it return null instead of the item of the list
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",[self.list objectAtIndex:indexPath.row]);
}
thank you to everyone for its thoughts
Alessandro
Here below the code, the list is correctly displayed so the outlet and protocol method are set up (delegate, datasource).
The method tableview:didSelectRowAtIndexPath: it prints out (null) instead of the right element in the list
//
// MainViewController.m
// ProvaTable
//
// Created by Alessandro on 12/6/12.
// Copyright (c) 2012 Alessandro. All rights reserved.
//
#import "MainViewController.h"
#interface MainViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) NSArray *listOfObjects;
#end
#implementation MainViewController
#synthesize tableView = _tableView;
#synthesize listOfObjects = _listOfObjects;
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.listOfObjects = [NSArray arrayWithObjects:#"Object 1",#"Object 2", nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listOfObjects count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell Item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
}
// Set up the cell...
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15];
cell.textLabel.text = [self.listOfObjects objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",[self.listOfObjects objectAtIndex:indexPath.row]);
}
#end

Resources